Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

A month, day or hour is padded to two digits

Write the number without the zero.

Fix
// fails to compile
// $month = 08;

$month = 8;
printf('%02d', $month); // prints 08
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to PHP 7.0 to 8.6, 0o octal prefix: PHP 8.1 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
php -l report.php
sed -n '14p' report.php

Then find the line you see in this list and open its fix:

  1. 1A month, day or hour is padded to two digitsThe line holds a value such as 08 or 09 that counts something: a month, a day, an hour or a minute
  2. 2A date was typed without quotesThe line looks like $start = 2024-08-15; or passes 2024-09-01 straight into a function call
  3. 3A code with leading zeros is stored as an integerThe 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 14
  • Errors 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.

Check
grep -nE '(^|[^0-9.])0[0-9]*[89]' report.php

Fix

Write the number without the zero. When the zero matters only for display, add it while formatting the output, not in the literal.

Fix
// fails to compile
// $month = 08;

$month = 8;
printf('%02d', $month); // prints 08

Verify

The lint passes.

Verify
php -l report.php   # No syntax errors detected in report.php

Confirmed by php.net

Did this fix it?

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.

Check
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.

Fix
$start = '2024-08-15';
$date  = new DateTimeImmutable('2024-08-15');

Verify

The variable now holds the string, not a number.

Verify
php -r "\$start = '2024-08-15'; var_dump(\$start);"   # string(10) "2024-08-15"

Confirmed by php.net

Did this fix it?

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.

Check
grep -nE "=> *0[0-9]+" config/*.php

Fix

Store the code as a string. Taking the zero away would change the code itself.

Fix
$warehouses = [
    'princeton' => '08540',
    'boston'    => '02134',
];

Verify

The file lints, and the stored code keeps its zero.

Verify
php -l config/warehouses.php

Confirmed by php.net

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.