php PHP中二进制的前缀是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2507208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:44:13  来源:igfitidea点击:

What's the prefix for binary in PHP?

phpsyntaxbinary

提问by user198729

It's neither 0xnor 0; what is it? Is there?

它既不是0x也不是0; 它是什么?在那儿?

回答by Pascal MARTIN

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

从 PHP 5.4+ 开始,二进制数的前缀是:

0b



对于早期版本,没有这样的前缀。相反,您可以使用0x0x, 表示十六进制。

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

有关更多信息,请参阅PHP 手册整数部分

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

尽管如此,如果您确实需要在 PHP 5.4 之前使用二进制写入值,您可以使用该bindec函数,该函数接受一个包含二进制的字符串,并返回相应的值。

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.

但是请注意,您不应该经常这样做:每次执行脚本时调用一个函数来执行此操作对性能来说非常糟糕^^
相反,使用十六进制写入值确实是一个更好的解决方案,其中每个数字编码为 4位。

回答by Brad Koch

New in PHP 5.4is 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+.

PHP 5.4中的新功能是二进制前缀0b. 不过在使用时要考虑便携性;您需要能够保证服务器运行 PHP 5.4+。

回答by codaddict

From PHP 5.4+ there is, in fact, a prefix: 0b.

从 PHP 5.4+ 开始,实际上有一个前缀:0b.

Current info in PHP Docs:

PHP 文档中的当前信息:

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.

自 PHP 5.4.0 起,二进制整数文本可用。

要使用八进制表示法,请在数字前面加上 0(零)。要使用十六进制表示法,请在数字前加上 0x。要使用二进制表示法,请在数字前加上 0b。



Mar 24 '10 at 11:31 (original answer):

2010 年 3 月 24 日,11:31(原始答案):

I don't think there is one. Prefix 0if for octal and 0xis for hexadecimal.

我不认为有一个。前缀0if 表示八进制,0x表示十六进制。

From PHP docs:

来自PHP 文档

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.

整数可以用十进制(基数 10)、十六进制(基数 16)或八进制(基数 8)表示法指定,可以选择在前面加上符号(- 或 +)。

要使用八进制表示法,请在数字前面加上 0(零)。要使用十六进制表示法,请在数字前加上 0x。

回答by Christoph

Check the manual: PHP only supports decimal, hexadecimal and octal integer notation, but you can use bindec()instead.

查看手册:PHP 仅支持十进制、十六进制和八进制整数表示法,但您可以使用bindec()代替。

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.

另外,请记住,每个十六进制数字代表 4 位,因此很容易在二进制表示法和十六进制之间进行转换。八进制也是如此,但由于各种原因,它在实践中的使用频率较低。