20
votes

Finding the answer to this is turning out to be much more difficult than I would have thought. Since I don't have a clue what you'd call this, it's hard to run a Google search since it will ignore those characters.

I tried browsing the PHP Assignment Operators page, and even the other operators pages, and found nothing that told me exactly what they do. I don't just want to guess based on the single function I have that uses it. So what exactly do the '&=' and '=&' operators do?

All I know is it sets a variable, which would be the '=' part, so I really need to know what the '&' part is doing.

Please don't say the obvious; I need someone to explain exactly what they do. I know one of them is 'bitwise', but that doesn't mean anything to me.

4
They are completely different operators as the answers reflect.Matt Mitchell
I had the same problem trying to look this up myself. The documentation could be better organized, but you basically have to look on the Bitwise Operator page and the What References Do page. Also, I think assigning by reference is usually written $a = &$b, but I guess the space between the = and & are optional. php.net/manual/en/language.references.whatdo.phpLèse majesté
I prefer using $a = &$b. It's much clearer, because the & does not belong to the equal sign, but the variable actually. It's similar to a --> 0 "goes to" operator, which in reality is a-- > 0. stackoverflow.com/q/1642028/985454 So much to the confusement.Qwerty

4 Answers

36
votes

=& assigns by reference

$a = 1;
$b =& $a;
$a++;
echo $b; // 2

From PHP Manual on References:

References in PHP are a means to access the same variable content by different names.


&= is a bitwise AND assignment

$a = 1;
$a &= 1; // is the same as
$a = $a & 1;
echo $a; // 1

From Wikipedia on Bitwise AND:

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example:

    0101
AND 0011
  = 0001

EDIT: For a practical example on bitwise operations, see my answer to Bitwise Operations in PHP

10
votes

=& is assigning by reference.

It assigns a variable not by value but by reference.

Example:

$a = 'foo';
$b =& $a;

$b = 'bar';

echo $a;

prints bar because $b has a reference to $a and therefore changing $b also changes the value of $a.


&= is bitwise AND.

Example:

$a = 4 // binary representation: 100
$b = 1 // binary representation: 001

Then $a &= $b is just short for $a = $a & $b and means: Take every bit and perform the AND operation, that is:

0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
0 & 0 = 0

Therefore

     1 0 0 
AND  0 0 1
     -----
     0 0 0

=> $a = 0 // bit representation 0 ;)
2
votes
&=

is the bitwise "AND" assignment operator. It performs an "AND" on the variable and stores the result. (more information is in Bitwise Operators and more general information is in Bitwise Operations in C ).

The

=&

operator is an assignment by reference, which makes the variable point not to the value of the other variable or constant, but rather to that memory location (more information is in What References Are).

1
votes

'&=' and '=&' are very different operators.

'&=' is a bitwise assignment operator:

$var = false;
$var &= foo(); // will call foo()
$var = false & foo(); // will call foo()
$var = $var && foo(); // will not call foo()

'=&' returns a reference:

$a = $b; //$a points to $b
$a =& $b; //$a does NOT point to $b... both point to the same thing.