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

时间:2021-09-07 20:19:19
$binary = b'Binary string';

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

将字符串创建为二进制文件会产生什么后果?

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

我在文档中找不到任何关于此的提示。通过language_scanner查看这个小小的好奇心。

2 个解决方案

#1


18  

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

这是永远不会发布的PHP版本6的前向兼容性令牌,它应具有本机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.

在PHP6中,默认情况下字符串是unicode,函数在它们的unicode字符级别上运行。这个“b”表示“二进制字符串”,即非单字符串,函数在字节级操作。

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

这在PHP!= 6中没有任何影响,其中所有字符串都是二进制的。

#2


2  

Binary casting is available since 5.2.1 but will not take effect until 6.0 when unicode strings also take effect.

二进制转换自5.2.1起可用,但在unicode字符串生效时才会生效。

Which explains why this does nothing special right now for me on a server using 5.2.6:

这就解释了为什么在使用5.2.6的服务器上这对我来说没有什么特别之处:

<?php
$t = b"hey";
var_dump($t);
//string(3) "hey"

$s = (binary)"hey";
var_dump($s);
//string(3) "hey"
?>

#1


18  

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

这是永远不会发布的PHP版本6的前向兼容性令牌,它应具有本机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.

在PHP6中,默认情况下字符串是unicode,函数在它们的unicode字符级别上运行。这个“b”表示“二进制字符串”,即非单字符串,函数在字节级操作。

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

这在PHP!= 6中没有任何影响,其中所有字符串都是二进制的。

#2


2  

Binary casting is available since 5.2.1 but will not take effect until 6.0 when unicode strings also take effect.

二进制转换自5.2.1起可用,但在unicode字符串生效时才会生效。

Which explains why this does nothing special right now for me on a server using 5.2.6:

这就解释了为什么在使用5.2.6的服务器上这对我来说没有什么特别之处:

<?php
$t = b"hey";
var_dump($t);
//string(3) "hey"

$s = (binary)"hey";
var_dump($s);
//string(3) "hey"
?>