Main Content

Passing information into and out of PHP functions

Archive - Originally posted on "The Horse's Mouth" - 2005-06-07 08:19:22 - Graham Ellis

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. 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.

Return Values. 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.

Default parameters. 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.

Globals. 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!

Super-Globals. 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.

Statics. 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.