Parse error: invalid numeric literal

When you run this code below, the following error appears:

The code:

<?php $a = array(00001, 00008, 00009, 00012); print_r($a); ?> 

Error:

Parse error: invalid numeric literal.

Why did this problem arise and how to solve it?

+6
source share
3 answers

This comes from changes made to how integers, in particular octal numbers, are processed in PHP7 (as oppsoed for PHP5).

From the documentation (from PHP7 migration)

Invalid octal literals

Previously octal literals containing invalid numbers were truncated silently (0128 was taken as 012). An invalid octal literal will now cause a parsing error.

From the documentation of integers

Prior to PHP 7, if an invalid digit was specified in an octal integer (for example, 8 or 9), the rest of the number was ignored. Starting with PHP 7, a parsing error occurs.

Either use them as strings or actual integers

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

This is because all numbers starting with 0 are considered octal values ​​that have an upper limit of 8 digits per position (0-7). As indicated in the PHP manual , instead of silently discarding invalid numbers, they now (7.x) trigger the warning above.

Why are you writing your numbers like this? If the leading zeros are significant, then this is not the number you have, but a string. If you need to do calculations on them as if they were numbers, then you need to add leading zeros when outputting values ​​to the client.
This can be done using printf() or sprintf() as follows:

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

Please see the manual for more examples .

0
source

Once a visible numeric literal is detected as an invalid numeric literal.

This is regression since php5.4

You can fix this by changing the array to:

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

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

0
source

Source: https://habr.com/ru/post/1012554/


All Articles