Skip to content
Latchkey

PHP "Parse error: syntax error, unexpected" in CI

PHP could not parse a file. Either there is a genuine syntax mistake, or you used syntax newer than the PHP version running in CI - enums, named arguments, constructor promotion, or readonly properties on a too-old interpreter.

What this error means

The script fails before any code runs with "Parse error: syntax error, unexpected ...", pointing at a line. This is a compile-time error: PHP cannot even tokenize the file, so nothing executes.

PHP error
PHP Parse error:  syntax error, unexpected token "readonly", expecting
variable in /app/src/Money.php on line 7

Common causes

A genuine syntax mistake

A missing semicolon, unbalanced brace/parenthesis, or stray character. The "unexpected" token usually appears just after the real mistake.

Syntax newer than the runner’s PHP

Enums (8.1), readonly properties (8.1), constructor promotion (8.0), and named arguments (8.0) raise a parse error on an older PHP that cannot tokenize them.

How to fix it

Lint the file to find the exact spot

PHP’s built-in lint reports the first parse error without executing the file.

Terminal
php -l src/Money.php
# lint everything that changed
git diff --name-only --diff-filter=ACM | grep '\.php$' | xargs -n1 php -l

Match the PHP version to the syntax

  1. Identify the minimum PHP for the construct (e.g. enum → 8.1).
  2. Pin CI to that version or newer with setup-php.
  3. Confirm with php -v in the same job.

How to prevent it

  • Run php -l (or a linter) on changed files early in CI.
  • Pin a PHP version that supports every language feature you use.
  • Set the "php" constraint in composer.json to your true minimum.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →