数字具有前导零的奇怪行为 [英] Strange behaviour with numbers that have a leading zero

查看:235
本文介绍了数字具有前导零的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些整数的PHP代码,一切正常,除非我有 08 0X as整数。当我把它们放在引号中时一切正常。

I have some PHP code with some integers and all works fine, except when I have 08 or 0X as integer. It all works fine when I put them in quote.

示例数字:

2      //Works fine
08     //Doesn't work
012    //Doesn't work
"08"   //Works fine again
"012"  //Works fine again

谁能告诉我背后的原因?

Can anyone tell me the reason behind it?

推荐答案

如果您只是编写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.

这篇关于数字具有前导零的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆