开发者

In PHP, what is a binary string (b'xxxx')?

What does开发者_Python百科 b'' ("binary string") mean in the PHP statement $str=b'xxxxxx';?


The PHP language reference describes the distinction between unicode strings and native binary strings, denoted with b'this is a binary string'

It doesn't seem to be a method of representing binary numbers.

The notation "is available since PHP 5.2.1. However, it will only have effect as of PHP 6.0.0"


Binary strings were introduced in PHP 5.2.1 but were expected to become effective only starting from PHP 6. It was an attempt to distinguish between Unicode and binary strings. The former being the new string format, the latter being the current format but changed to "b" notation.

As you may know, PHP 6 had been abandoned and string format didn't change. The "b" notation is just a relic from that era.

'foobar' === b'foobar'; // true


So nowadays (PHP 5 and 7), binary string is the only string format. The character encoding (which can be multibyte) is managed at the "upper level", by your text editor, web browser, etc. You can consider a binary string to be a string consisting of the full 0-255 range of byte values.

A string with nonprintable characters and such, can be manipulated in PHP as any other string, but you'll get garbage displaying it. Also some characters have special meaning: \0 marks string termination in C and SQL, there are control characters, etc. So as soon as you use them outside of PHP you are likely to encounter troubles.

For example, random_bytes() produces such strings.


"binary string" is what we have in php right now - a sequence of bytes, which (as opposed to C language) can also include nul byte. This is (or will be, as of php6) different from unicode strings, which are sequences of two-byte characters.


$number = decbin(59);
echo $number . "\n"; //111011
$number = bindec($number);
echo $number . "\n"; //59

decbin(int $number): Returns a string containing a binary representation of the given number argument.

bindec(string $bstring): Returns the decimal equivalent of the binary number in bstring argument.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜