What Do the "=&" and "&=" Operators in PHP Mean

What do the =& and &= operators in PHP mean?

$a &= $b is short for $a = $a & $b which is the bitwise-and operator.

$a =& $b assigns $a as a reference to $b.

What do the '&=' and '=&' operators do?

=& 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

What is =& in PHP?

The = and the & should* have a space between them - they're two different operators. The & means get a reference to this.

The -> is for object member access - this means assign 'xmlrpcvals' to the return_type member of $client.

* see comments for clarification

PHP & operator

Those are references, and they are similar to "pointers" in C or C++.

More information is in the PHP manual.

In this case, since $db_hosts is empty, the construct $db_hosts[$db_item['hostid']] will create a new array with an empty item on the index of $db_item['hostid'] and return the item's reference, making $db_host act as an 'alias' for whatever $db_hosts[$db_item['hostid']] is currently.

What is the meaning of |= in php?

This is bitwise OR operator

$var1 |= $var2; is equal to $var1 = $var1 | $var2;

Reference — What does this symbol mean in PHP?

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

  • Understanding Incrementing

What does =& mean?

It's to assign the value returned by the function as a reference.

Is there any difference between and and && operators in PHP?

In your case, && and and do the same thing, but take a look at the operator precedence. You can see that && and and are not on the same level, so mixing that could give you unexpected results in some cases - I recommend always using &&, but that's your choice.



Related Topics



Leave a reply



Submit