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

前端 未结 4 1417
抹茶落季
抹茶落季 2021-01-03 23:24

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 s

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 00:12

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

提交回复
热议问题