Php/docs/language.references.pass
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.
The following things can be passed by reference:
-
Variables, i.e.
foo($a)
References returned from functions, i.e.:
<?phpfunction foo(&$var){ $var++;}function &bar(){ $a = 5; return $a;}foo(bar());?>
See more about returning by reference.
No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid:
<?phpfunction foo(&$var){ $var++;}function bar() // Note the missing &{ $a = 5; return $a;}foo(bar()); // Produces a noticefoo($a = 5); // Expression, not variablefoo(5); // Produces fatal errorclass Foobar{}foo(new Foobar()) // Produces a notice as of PHP 7.0.7 // Notice: Only variables should be passed by reference?>