Parse error: Invalid numeric literal

Rana Ghosh picture Rana Ghosh · Nov 22, 2016 · Viewed 37.8k times · Source

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?

Answer

Qirel picture Qirel · Nov 22, 2016

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

From the documentation (from PHP7 migration)

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.

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.

Either use them as strings, or actual integers

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