php - 解析错误 : Invalid numeric literal

标签 php arrays php-7 octal

运行下面这段代码时出现以下错误:

代码:

<?php
    $a = array(00001, 00008, 00009, 00012);
    print_r($a);
?>

错误:

Parse error: Invalid numeric literal.

为什么会出现这个问题,我该如何解决?

最佳答案

这是因为在 PHP7 中处理整数(特别是八进制)的方式发生了变化(与 PHP5 不同)。

来自文档(来自 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.

来自整数的文档

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.

将它们用作字符串或实际整数

$a = array(1, 8, 9, 12); // Integers
$a = array("00001", "00008", "00009", "00012"); // Strings

关于php - 解析错误 : Invalid numeric literal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40735963/

相关文章:

javascript - Post 参数不会从 ajax 请求发送到 PHP

php - 如果 (!empty($_POST)) 不工作

php - 选择列文本包含数组值的行

php - 数组中的唯一关联索引

c - 使用 for 循环的数组输入

php - 离开绝对的日子

python - 使用numpy的genfromtxt用python加载三角矩阵

PHP7 method_exists 未捕获错误 : Function name must be a string

php - Laravel Artisan 使用内存挂起直至卡住

php - Apache PHP 错误 AH00052 段错误 11