I recently came across a function defined as follows:
function *functionname* (&$parameter1, &$parameter, $parameter3)
Could someone tell me what does the & mean in front of $parameter1
and $parameter2
please? Thanks for your help!
I recently came across a function defined as follows:
function *functionname* (&$parameter1, &$parameter, $parameter3)
Could someone tell me what does the & mean in front of $parameter1
and $parameter2
please? Thanks for your help!
That means passing by reference.
ref: http://php.net/manual/en/language.references.pass.php
Normally, PHP is a pass by value
but if you wish to perform pass by reference you can do the &$var
<?php
function foo(&$var)
{
$var++;
}
function bar() // Note the missing &
{
$a = 5;
return $a;
}
foo(bar()); // Produces fatal error as of PHP 5.0.5, strict standards notice
// as of PHP 5.1.1, and notice as of PHP 7.0.0
foo($a = 5); // Expression, not variable
foo(5); // Produces fatal error
?>
http://php.net/manual/en/functions.arguments.php
To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition:
You are assigning that array value by reference.
passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this copy, so if you change it in the function it won't be changed outside of it