Assignment operator in PHP, =&

It’s not deprecated and is unlikely to be. It’s the standard way to, for example, make part of one array or object mirror changes made to another, instead of copying the existing data.

It’s called assignment by reference, which, to quote the manual, “means that both variables end up pointing at the same data, and nothing is copied anywhere”.

The only thing that is deprecated with =& is “assigning the result of new by reference” in PHP 5, which might be the source of any confusion. new is automatically assigned by reference, so & is redundant/deprecated in$o = &new C;, but not in $o = &$c;.

Since it’s hard to search, note that =& (equals ampersand) is the same as = & (equals space ampersand) and is often written such that it runs into the other variable like $x = &$y['z']; or $x = &$someVar (ampersand dollar sign variable name). Example simplified from the docs:

$a = 3;
$b = &$a;
$a = 4;
print "$b"; // prints 4

Here’s a handy link to a detailed section on Assign By Reference in the PHP manual. That page is part of a series on references – it’s worth taking a minute to read the whole series.

Leave a Reply

Your email address will not be published. Required fields are marked *