In PHP, named blocks of code are referred to as "functions". By default, variables within functions are created each time the function is run, and do not conflict with any other similarly named functions elsewhere in your code. That's good news for coders of medium sized and larger applications, who want to be able to pull in code from included files written by others without the need to avoid any variable names that the others may have used, but it does mean that the language has to provide mechanisms to get information into and back from functions so that they don't run in complete isolation from one another.
Here's a review of the ways of sharing data between functions, or saving data from one use of a function to the next.
. Parameters are passed in to functions in a list in round brackets after the call, and the names that will be used for the parameters are declared in a similar length list within the function definition. Note that by default the values passed to a function are COPIED in to those variables, so that any changes made are NOT reflected in the source variable in the main code.
. At the end of a function, you can choose to return a variable or an expression, and that will be saved into a variable in the main code is you choose to assign it, or you can test it in a conditional statement or include it in another expression if you wish. Although only one variable can be passed back, it can be an array - so that in effect you can pass back a collection of values.
Parameters and return values are the recommended way of passing most information in and out of functions. The other facilities listed below all have their uses, but they should not be used to excess.
. If you declare a function with a defaulted parameter (e.g. if you say $abc=17.5 rather than just $abc in the function definition), you're then allowed to call the function omitting the parameter and if you do it will assume the value given. Very useful if you have a function that has parameter(s) that are only needed on some occasions.
Parameters by name
. If you declare a function with a leading & character in front of the $ - so &$abc rather than $abc for example - then the parameter will not be copied in to the function, but rather the internal variable name will be used as another name for the same piece of data in the calling code. This means that if you alter the variable in the function, you're also altering the variable with which it is connected in the main code. A very useful facility, but very dangerous if the calling programmer isn't aware that your function may change his settings.
. If you declare a variable to be global in a function, then that variable is the same as the variable of the same name in your main code. There's no need to declare anything as global in the main code - things always ARE global there. Use this facility sparingly, as it can cause functions to be less portable now that they work with fixed name values, and it can bring variable name conflicts one step closer. If you want to share variables between several functions, you can
declare them to be global in each function and it will work - just be careful you don't use the same variable name for something else in the main code!
. PHP provides you with a number of variables such as $_SESSION and $_REQUEST which as known as "superglobals". They're always available in any functions without the need to declare them, and a reference to a superglobal within a function is the same as a reference to a superglobal in the main code.
. Usually, a function's local variables are lost when you exit from a function and then they'll be re-initialised next time the function is called. That's usually the behaviour that you want, since most of the time you're likely to be calling functions several times with each of the calls being unrelated to previous calls. However, if you want a variable to persist from once call to the nest, you can declare it as static. (written 2005-06-07, updated 2008-10-11)
|Graham:||As an addendum ... I was asked yesterday if you can define your own superglobals. I gave the poliicians answer - "why on earth would you want to?" - and didn't address the questions directly. A good answer since I didn't want to encourage the class to even try, but in reallity the answer is "not exactly". Superglobals are reserved PREDEFINED variables ... but there's nothing to stop you adding an element into the superglobal array $GLOBALS (or any of the others if you really want)|
(comment added 2005-06-09 07:57:15)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesH105 - PHP - Functions 
Coding efficiency - do not repeat yourself! - (2010-11-02) 
Passing a variable number of parameters in to a function / method - (2010-08-20) 
Improving your function calls (APIs) - General and PHP - (2010-04-24) 
Adding extensions to PHP Open Source applications - callbacks - (2010-03-17) 
Static variables and ampersands in PHP - (2010-02-10) 
A variable number of arguments in a PHP function - (2009-11-02) 
Global - Tcl, PHP, Python - (2008-09-03) 
Static variables in PHP - (2007-10-05) 
Clean my plate, but keep my wine bottle. (PHP; Static) - (2007-09-18) 
is there a lookup function in php? - (2007-07-15) 
Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24) 
A better alternative to cutting and pasting code - (2007-04-26) 
PHP - static declaration - (2007-01-04) 
Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21) 
A lazy programmer is a good programmer - (2006-09-15) 
Do not duplicate your code - (2006-06-23) 
Don't repeat code - use loops or functions - (2005-08-21) 
Functions and commands with dangerous names - (2005-08-11) 
Code and code maintainance efficiency - (2005-06-08) 
Call by name v call by value - (2005-05-11) 
There is a function in PHP to do that - (2005-02-21) 
Variable Scope - (2004-10-22)
Some other Articles
Should I use structured or object oriented?The evening after the courseHappy Birthday, PHPPassing information into and out of PHP functionsOO techniques are hard to teachthe array returned by preg_match_allTargetted AdvertisingSad prioritiesSymbolic links and hard links