编程语言
首页 > 编程语言> > php – 解析错误:无效的数字文字

php – 解析错误:无效的数字文字

作者:互联网

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

码:

<?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

> PHP7与PHP5的示例:https://3v4l.org/GRZF2
> http://php.net/manual/en/language.types.integer.php
>手册:http://php.net/manual/en/migration70.incompatible.php

标签:php,arrays,php-7,octal
来源: https://codeday.me/bug/20190930/1836698.html