The ampersand ( & ) before a variable ( & $foo ) overrides pass by value to specify that you want to pass the variable by reference instead.
For example if you have this:
function doStuff($variable) {
$variable++;
}
$foo = 1;
doStuff($foo);
echo $foo;
// output is '1' because you passed the value, but it doesn't alter the original variable
doStuff( &$foo ); // this is deprecated and will throw notices in PHP 5.3+
echo $foo;
// output is '2' because you passed the reference and php will alter the original variable.
It works both ways.
function doStuff( &$variable) {
$variable++;
}
$foo = 1;
doStuff($foo);
echo $foo;
// output is '2' because the declaration of the function requires a reference.
It's a pass by reference. The variable inside the function "points" to the same data as the variable from the calling context.
Basically if you change
$var
inside the function, it gets changed outside. For example:you are passing $var as reference, meaning the actual value of $var gets updated when it is modified inside boo function
example:
If any function starts with ampersand(&), It means its call by Reference function. It will return a reference to a variable instead of the value.
The ampersand ( & ) before a variable ( & $foo ) overrides pass by value to specify that you want to pass the variable by reference instead.
For example if you have this:
It works both ways.
It accepts a reference to a variable as the parameter.
This means that any changes that the function makes to the parameter (eg,
$var = "Hi!"
) will affect the variable passed by the calling function.