PHP will not compile a number such as 08 or 09
Your error
Parse error: Invalid numeric literal in /var/www/app/report.php on line 141 more line it can showFix it fast
Most common cause, 1 of 3A month, day or hour is padded to two digits
Write the number without the zero.
// fails to compile
// $month = 08;
$month = 8;
printf('%02d', $month); // prints 08Short answer
Parse error: Invalid numeric literal means the PHP scanner met a number that starts with 0 and contains an 8 or a 9. A leading zero makes the literal octal, octal has no such digits, and PHP 7.0 and later refuse to compile the file. Drop the zero, or put quotes around the value. Applies to PHP 7.0 through 8.6.
Which cause do you have?
Lint the file. The message names the line, and that line holds a number written with a leading zero; what the number stands for decides which fix below applies.
php -l report.php
sed -n '14p' report.phpThen find the line you see in this list and open its fix:
- 1A month, day or hour is padded to two digits
The line holds a value such as 08 or 09 that counts something: a month, a day, an hour or a minute - 2A date was typed without quotes
The line looks like $start = 2024-08-15; or passes 2024-09-01 straight into a function call - 3A code with leading zeros is stored as an integer
The value is a postal code, phone number, account or product code such as 08540, usually in an array or config file
What does the error look like?
Parse error: Invalid numeric literal in /var/www/app/report.php on line 14Errors parsing report.php
Cause 1 of 3
A month, day or hour is padded to two digits
You seeThe line holds a value such as 08 or 09 that counts something: a month, a day, an hour or a minute
Check
Invalid numeric literal here comes from padding a quantity with a zero. PHP's integer grammar reads 0 followed by digits as octal (0123 is 83 decimal), and the scanner throws a ParseError as soon as such a literal contains 8 or 9. Values 01 to 07 compile and keep their meaning, which is why this code ran until August, September or an 08:00 slot came along.
grep -nE '(^|[^0-9.])0[0-9]*[89]' report.phpFix
Write the number without the zero. When the zero matters only for display, add it while formatting the output, not in the literal.
// fails to compile
// $month = 08;
$month = 8;
printf('%02d', $month); // prints 08Verify
The lint passes.
php -l report.php # No syntax errors detected in report.phpConfirmed by php.net
Cause 2 of 3
A date was typed without quotes
You seeThe line looks like $start = 2024-08-15; or passes 2024-09-01 straight into a function call
Check
Without quotes, 2024-08-15 is arithmetic: 2024 minus 08 minus 15, and the 08 hits the octal rule. The same mistake with a date such as 2024-05-01 compiles and quietly evaluates to the integer 2018, so search the codebase for both kinds once you find one.
grep -rnE '[=(,] *[0-9]{4}-[0-9]{2}-[0-9]{2} *[;,)]' --include='*.php' .Fix
Quote the date, or build a date object from the quoted string.
$start = '2024-08-15';
$date = new DateTimeImmutable('2024-08-15');Verify
The variable now holds the string, not a number.
php -r "\$start = '2024-08-15'; var_dump(\$start);" # string(10) "2024-08-15"Confirmed by php.net
Cause 3 of 3
A code with leading zeros is stored as an integer
You seeThe value is a postal code, phone number, account or product code such as 08540, usually in an array or config file
Check
Postal codes and phone numbers are labels, so their zeros are part of the value. As a bare literal, 08540 does not compile, and one like 02134 compiles into the octal value 1116 with no warning. Before PHP 7.0 the bad digit and the rest of the number were silently dropped, so code ported from PHP 5 may show this for the first time.
grep -nE "=> *0[0-9]+" config/*.phpFix
Store the code as a string. Taking the zero away would change the code itself.
$warehouses = [
'princeton' => '08540',
'boston' => '02134',
];Verify
The file lints, and the stored code keeps its zero.
php -l config/warehouses.phpConfirmed by php.net
How do I stop it coming back?
- Run php -l over every changed PHP file in CI; since PHP 8.3 one call can check several files.
- Keep dates, postal codes and phone numbers as strings in code and config, and parse dates with DateTimeImmutable.
- When you really mean octal, for example a chmod mode, write the 0o prefix from PHP 8.1 (0o755) so a reader sees the intent.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.