This is the Refresh Operator .
According to PHP
References, in PHP, means accessing the same variable content across multiple names
See an example:
$a = 1;
$b = $a;
$a = 2;
var_dump($a, $b); // a => 2, b => 1
Now an example with the reference operator:
$a = 1
$b =& $a;
$a = 2;
var_dump($a, $b)/ // a= > 2, b => 2
That is, $b
points to the value of $a
. It does not have an independent value, but it points to the memory location where the value of $a
has been saved.
In the case of functions, the &
operator can be used in two ways:
I'll give you an example of passing by reference in functions:
$meuValor = [1, 2, 3];
function my_function(&$var, $add){
$var[] = $add;
}
my_function($meuValor, 6);
print_($meuValor); // [1, 2, 3, 6];
Notice that the value of $meuValor
has been changed, without the return
within the function and without the reassignment of it by the return of my_function
.
This happens because $var
passes the reference of the variable passed by parameter; that is, the variable $meuValor
.
The use of the new
operator has been discouraged, and if I am not mistaken, it generates a E_STRICT
error when trying to do it.
This was in versions prior to PHP 5.
In new versions of PHP, objects are passed by reference by default.