php 解析错误:无效的数字文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40735963/
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
Parse error: Invalid numeric literal
提问by Rana Ghosh
I have the following error while running this code below:
运行以下代码时出现以下错误:
Code:
代码:
<?php
$a = array(00001, 00008, 00009, 00012);
print_r($a);
?>
Error:
错误:
Parse error: Invalid numeric literal.
解析错误:无效的数字文字。
Why this issue occurred and how do i solve this?
为什么会出现这个问题,我该如何解决?
回答by Qirel
This comes from the changes made to how integers, specifically octals, are handled in PHP7 (as oppsoed to PHP5).
这来自对整数(特别是八进制)在 PHP7 中处理方式所做的更改(与 PHP5 相对)。
From the documentation (from PHP7 migration)
来自文档(来自 PHP7 迁移)
Invalid octal literals
Previously, octal literals that contained invalid numbers were silently truncated (0128 was taken as 012). Now, an invalid octal literal will cause a parse error.
无效的八进制文字
以前,包含无效数字的八进制文字会被静默截断(0128 被视为 012)。现在,无效的八进制文字将导致解析错误。
From the documentation of integers
从整数文档
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.
在 PHP 7 之前,如果在八进制整数(即 8 或 9)中给出了无效数字,则其余数字将被忽略。自 PHP 7 起,会发出解析错误。
Either use them as strings, or actual integers
将它们用作字符串或实际整数
$a = array(1, 8, 9, 12); // Integers
$a = array("00001", "00008", "00009", "00012"); // Strings
- Example of PHP7 vs PHP5: https://3v4l.org/GRZF2
- http://php.net/manual/en/language.types.integer.php
- Manual: http://php.net/manual/en/migration70.incompatible.php
- PHP7 与 PHP5 的示例:https://3v4l.org/GRZF2
- http://php.net/manual/en/language.types.integer.php
- 手册:http: //php.net/manual/en/migration70.incompatible.php
回答by ChristianF
This is because all numbers starting with 0 is considered octal values, which has an upper limit of 8 digits per position (0-7). As stated in the PHP manual, instead of silently dropping the invalid digits they now (7.x) produce the above warning.
这是因为所有以 0 开头的数字都被视为八进制值,每个位置的上限为 8 位 (0-7)。正如PHP 手册中所述,他们现在 (7.x) 不是默默地丢弃无效数字,而是产生上述警告。
Why are you writing your numbers like that though? If the leading zeroes are significant, then it's not a number you have but a string. Should you need to do calculations on those as if they were numbers, then you need to add the leading zeroes when outputting the values to the client.
This can be done with printf()
or sprintf()
like this:
你为什么要这样写你的数字?如果前导零很重要,那么它不是您拥有的数字,而是一个字符串。如果您需要像数字一样对它们进行计算,那么您需要在将值输出到客户端时添加前导零。
这可以通过printf()
或sprintf()
像这样完成:
$number = 5;
printf ("%05d", $number);
Please see the manual for more examples.
回答by Ashish Kumar
Sometime an apparently valid numeric literal is being detected as an invalid numeric literal.
有时,一个明显有效的数字文字被检测为无效的数字文字。
This is a regression since php5.4
这是自 php5.4 以来的回归
You can be fix this by changing the array to:
您可以通过将数组更改为:
$a =array(1,8,9,12);
$a = array('0001','0008','0009','0012'); //alternative method for fix
Reference: https://bugs.php.net/bug.php?id=70193