Anish Chandran Anish Chandran - 16 days ago 5
PHP Question

PHP operator |= explain

I have a code like this

<?php
$mask = 5;
$mask |= 10;
echo $mask;
?>


output of the above code is 15. But i cant understand the operator |=

Answer

$mask |= 10 means $mask = $mask | 10. '|' operator is called bitwise operator. Follow the example how bitwise operator works.

$mask =$mask | 10
      = 5 | 10
Firstly we have to convert the decimal to binary.
128 64 32 16 8 4 2 1
               1 0 1 =>5
             1 0 1 0 => 10
So for 5| 10
Output will be 8+4+2+1 =15

Here we need to add all decimal number that is under 1 bit.

Another example For 10 | 10

128 64 32 16 8 4 2 1
             1 0 1 0=>10
             1 0 1 0 => 10

So the result of 10 | 10 is

= 8+2 = 10

For more information visit this link http://www.w3resource.com/php/operators/bitwise-operators.php