1

Possible Duplicate:
Reference assignment operator in php =&

What does the =& assignment operator mean in PHP? I could not find any reference in PHP Manual in assignment section.

I saw it in a class instantiation, so I quite don't understand what's the difference between =& and solely =.

Community
  • 1
  • 1
romeroqj
  • 829
  • 3
  • 10
  • 21

3 Answers3

8

It means reference assignment.

There are two differences between = and =&.

First, = does not create reference sets:

$a = 1;
$b = $a;
$a = 5; // $b is still 1

On the other hand, the =& operator does create reference sets:

$a = 1;
$b =& $a;
$a = 5; // $b is also 5

Second, = changes the value of all variables in the reference set, while =& breaks the reference set. Compare the example before with this:

$a = 1;
$b =& $a;
$c = 5;
$a =& $c; // $a is 5, $b is 1
IMSoP
  • 89,526
  • 13
  • 117
  • 169
Artefacto
  • 96,375
  • 17
  • 202
  • 225
2

It's called a Reference Assignment. It makes the assigned-to variable point to the same value as the assigned-from variable.

In PHP 4, this was fairly common when assigning objects and arrays otherwise you would get a copy of the object or array. This was bad for memory management and also certain types of programming.

In PHP 5, objects and arrays are reference-counted, not copied, so reference assignment is needed much less often. Some programmers still use it 'just in case' PHP for some reason decides a copy makes sense there. But a reference assignment is still valid in other ways, such as with scalar variables, which are normally copied on assignment.

staticsan
  • 29,935
  • 4
  • 60
  • 73
-1

It's a reference assignment(deadlink) which is really two different operators.

The = is the assignment and the & accesses the value on the right by reference.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • You cannot "access a value by reference" in PHP. As Artefacto's answer says, this is a single operator which creates a *reference set*, where *the variables on both sides* are bound together. – IMSoP Jun 28 '22 at 09:32