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

前端 未结 4 1455
抹茶落季
抹茶落季 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:13

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

提交回复
热议问题