Search code examples
phparraysphp-7octal

Parse error: Invalid numeric literal


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?


Solution

  • 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