字符串字面量前面的 b 有什么作用?

2021-12-29 00:00:00 string syntax php
$binary = b'Binary string';

将字符串创建为 binary 有什么后果?

What consequences does it have to create a string as binary?

我在文档中找不到任何关于此的提示.刚刚在浏览 language_scanner 时发现了这个小小的好奇心.

I couldn't find any hint about that in the documentation. Just found this little curiosity while looking through the language_scanner.

推荐答案

这是一个前向兼容性令牌,适用于永远不会发布的 PHP 版本 6,它应该具有本机 unicode 支持.

This is a forward compatibility token for the never-to-be-released PHP version 6, which should have had native unicode support.

在 PHP6 中,字符串默认为 unicode,函数在 unicode 字符级别对其进行操作.这个b"的意思是二进制字符串",即非unicode字符串,函数在字节级别对它进行操作.

In PHP6, strings are unicode by default, and functions operate at the unicode character level on them. This "b" means "binary string", that is, a non unicode string, on which functions operate at the byte level.

这在 PHP != 6 中无效,其中所有字符串都是二进制的.

This has no effect in PHP != 6, where all strings are binary.

相关文章