Search code examples
functionconstantsphpbitwise-or

Why the phpinfo() function gives wrong result when one or more of the numerical constants bitwise values are passed as arguments?


I'm using PHP 7.2.8

I executed below code :

<?php
  phpinfo(2 | 8); //Bitwise OR operator is used
?>

Above code worked fine and give me the expected result.

Then I tried to pass the numerical constants bitwise values as arguments i.e. binary equivalents of numbers 2 and 8 as arguments. Please see the below code :

<?php
  phpinfo(00000010 | 1000000); //Bitwise OR operator is used
?>

I got unexpected output of above code(i.e. the second one where bitwise values of numerical constants are passed).

Why so?

Please refer This Link for the information of description on arguments to be passed to the phpinfo() function and the meaning of each and every numerical constants to be passed.

Please let me know where I'm making a mistake? I'm just trying to execute the code as described in the manual text.

Thank You.


Solution

  • To express numbers in binary in PHP, you must use the prefix 0b (e.g, 0b00000010).

    Without that prefix, these numbers are being interpreted in a way you didn't intend. 00000010 is interpreted as the octal representation of 8, and 1000000 is being read as one million, in decimal.