What’s the prefix for binary in PHP?

As of PHP 5.4+ the prefix for binary number is:

0b


For ealier version, there is no such prefix. Instead, you can use 0x, for hexadecimal.

For more informations, see the Integers section of the PHP manual.

Still, if you really need to write values using binary before PHP 5.4, you can use the bindec function, that takes a string containing the binary, and returns the corresponding value.

For example, the following portion of code :

echo bindec('10011');

Will get you :

19

But note you shouldn’t do that too often : calling a function to do that each time the script is executed is quite bad for performances ^^

Instead, it’s really a better solution to write your values using hexadecimal, where each digit codes for 4 bits.

Leave a Comment