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
  • 819
  • 3
  • 10
  • 19

3 Answers3

7

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
Artefacto
  • 93,596
  • 16
  • 191
  • 218
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,179
  • 4
  • 58
  • 73
0

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
  • 37,596
  • 11
  • 75
  • 105
Justin Niessner
  • 236,029
  • 38
  • 403
  • 530