What's the prefix for binary in PHP?
It's neither 0x
no开发者_高级运维r 0
; what is it? Is there?
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.
New in PHP 5.4 is the binary prefix, 0b
. Think about portability when using it though; you'll need to be able to guarantee the server runs PHP 5.4+.
From PHP 5.4+ there is, in fact, a prefix: 0b
.
Current info in PHP Docs:
Binary integer literals are available since PHP 5.4.0.
To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.
Mar 24 '10 at 11:31 (original answer):
I don't think there is one. Prefix 0
if for octal and 0x
is for hexadecimal.
From PHP docs:
Integer can be specified in decimal (base 10), hexadecimal (base 16), or octal (base 8) notation, optionally preceded by a sign (- or +).
To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x.
Check the manual: PHP only supports decimal, hexadecimal and octal integer notation, but you can use bindec() instead.
Also, keep in mind that each hex digit represents 4 bits, so it's easy to convert between binary notation and hex. The same is true for octal, but for various reasons it's used less often in practice.
精彩评论