PHP assignment operator =&
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 =.
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
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.
It's a reference assignment which is really two different operators.
The =
is the assignment and the &
accesses the value on the right by reference.
上一篇: 在PHP中按位运算?
下一篇: PHP赋值运算符=&