且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数字具有前导零的奇怪行为

更新时间:2023-02-25 21:02:22

如果您只是编写08和09(没有引号)或任何其他带前导0的数字,PHP认为您正在写一个八进制值,并且08和09是无效的八进制数。

If you simply write 08 and 09 (without quotes) or any other numeric with a leading 0, PHP believes you're writing an octal value, and 08 and 09 are invalid octal numbers.

http://www.php.net/manual/en/language.types.integer.php


语法

整数可以用十进制(基数10),十六进制(基数16),
八进制(基数8)指定)或二进制(基数2)表示法,可选地前面带有
符号( - 或+)。

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).

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

Binary integer literals are available since PHP 5.4.0.

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

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 7之前,如果在八进制整数(即8或9)中给出了无效数字,其余的这个数字被忽略了。从PHP 7开始,会发出一个解析错误。

Warning: Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.