Passing by Reference

You can pass a variable by reference to a function so the function
can modify the variable. The syntax is as follows:

<?phpfunction foo(&$var){$var++;}

$a=5;foo($a);// $a is 6 here?>

Note:
There is no reference sign on a function call - only on
function definitions. Function definitions alone are enough to
correctly pass the argument by reference. As of PHP 5.3.0,
you will get a warning saying that "call-time pass-by-reference" is
deprecated when you use & in foo(&$a);.

User Contributed Notes 7 notes

By removing the ability to include the reference sign on function calls where pass-by-reference is incurred (I.e., function definition uses &), the readability of the code suffers, as one has to look at the function definition to know if the variable being passed is by-ref or not (I.e., potential to be modified). If both function calls and function definitions require the reference sign (I.e., &), readability is improved, and it also lessens the potential of an inadvertent error in the code itself. Going full on fatal error in 5.4.0 now forces everyone to have less readable code. That is, does a function merely use the variable, or potentially modify it...now we have to find the function definition and physically look at it to know, whereas before we would know the intent immediately.

Some have noticed that reference parameters can not be assigned a default value. It's actually wrong, they can be assigned a value as the other variables, but can't have a "default reference value", for instance this code won't compile :

<?phpfunction use_reference( $someParam, &$param =& $POST ){ ...} ?>

But this one will work :

<?phpfunction use_reference( $someParam, &$param = null )?>

So here is a workaround to have a default value for reference parameters :

This function internally swaps the contents between two simple variables using 'passing by reference'.

Some programming languages have such a swap function built in, but PHP seems to lack such a function. So, one was created to fill the need. It only handles simple, single variables, not arrays, but it is still a very handy tool to have.

No value is actually returned by this function, but the contents of the indicated variables will be exchanged (swapped) after the call.*/

The notes indicate that a function variable reference will receive a deprecated warning in the 5.3 series, however when calling the function via call_user_func the operation aborts without fatal error.

This is not a "bug" since it is not likely worth resolving, however should be noted in this documentation.

Sometimes we need functions for building or modifying arrays whose elements are to be references to other variables (arrays or objects for instance). In this example, I wrote two functions 'tst' and 'tst1' that perform this task. Note how the functions are written, and how they are used.

<?php
function tst(&$arr, $r) {
// The argument '$arr' is declared to be passed by reference,
// but '$r' is not;
// however, in the function's body, we use a reference to
// the '$r' argument

array_push($arr, &$r);
// Alternatively, this also could be $arr[] = &$r (in this case)
}