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

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

问题描述

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

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天全站免登陆