27

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?

3
  • I have tried it on PHPFiddle and it was working fine... Commented Nov 22, 2016 at 7:28
  • 3
    @d.coder Try it in PHP7 and see what happens... ;-) Commented Nov 22, 2016 at 7:37
  • yes it's showing error in PHP 7 but not in PHP 5 Commented Nov 22, 2016 at 7:38

5 Answers 5

35

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
Sign up to request clarification or add additional context in comments.

Comments

6

This is because all numbers starting with 0 is considered octal values, which has an upper limit of 8 digits per position (0-7). As stated in the PHP manual, instead of silently dropping the invalid digits they now (7.x) produce the above warning.

Why are you writing your numbers like that though? If the leading zeroes are significant, then it's not a number you have but a string. Should you need to do calculations on those as if they were numbers, then you need to add the leading zeroes when outputting the values to the client.
This can be done with printf() or sprintf() like this:

$number = 5;
printf ("%05$1d", $number);

Please see the manual for more examples.

1 Comment

PHP date('m') = 07. In August, date('m') will be 08. if(date('m') == 08){} will result in the above error.
1

I was getting this error, and it was resolved through changing one of my numeric elements in a mixed array (PHP) from a number that started with zero 04191977 to 4191977. So, removing the zero that started the numeric element.

Comments

0

Sometime an apparently valid numeric literal is being detected as an invalid numeric literal.

This is a regression since php5.4

You can be fix this by changing the array to:

$a =array(1,8,9,12);   

$a = array('0001','0008','0009','0012'); //alternative method for fix

Reference: https://bugs.php.net/bug.php?id=70193

Comments

0
decimal     : [1-9][0-9]*(_[0-9]+)*|0

octal       : 0[0-7]+(_[0-7]+)*

from document regex for consider value as decimal and octal are given above

In this scenario with value is 00008, 00009 not pass validation for octal or decimal as well. since it is given error, parse parameter to string is solving problem partially.

Note: In PHP any number starts from zero considered as octal but 8 and 9 will not used in octal number resulting throw this error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.