解析错误:无效的数字文字 [英] Parse error: Invalid numeric literal

查看:36
本文介绍了解析错误:无效的数字文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following error while running this code below:

代码:

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

错误:

解析错误:数字文字无效.

Parse error: Invalid numeric literal.

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

Why this issue occurred and how do i solve this?

推荐答案

这来自于对整数(特别是八进制)在 PHP7 中的处理方式所做的更改(与 PHP5 相对).

This comes from the changes made to how integers, specifically octals, are handled in PHP7 (as oppsoed to PHP5).

来自文档(来自 PHP7 迁移)

From the documentation (from PHP7 migration)

无效的八进制文字

以前,包含无效数字的八进制文字会被静默截断(0128 被视为 012).现在,无效的八进制文字会导致解析错误.

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.

来自整数文档

在 PHP 7 之前,如果在八进制整数(即 8 或 9)中给出了无效数字,则其余数字将被忽略.从 PHP 7 开始,会出现解析错误.

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.

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

Either use them as strings, or actual integers

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

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