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 Parse error: syntax error, unexpected token "readonly", expecting
variable in /app/src/Money.php on line 7Common 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.
php -l src/Money.php
# lint everything that changed
git diff --name-only --diff-filter=ACM | grep '\.php$' | xargs -n1 php -lMatch the PHP version to the syntax
- Identify the minimum PHP for the construct (e.g. enum → 8.1).
- Pin CI to that version or newer with setup-php.
- Confirm with
php -vin 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 incomposer.jsonto your true minimum.