PHP "Parse error: syntax error, unexpected" in CI
PHP failed to parse a source file before running any code. The "unexpected X" token shows where parsing broke. In CI this is often valid newer syntax (enums, readonly, named args) running on an older PHP than locally.
What this error means
A step fails with "PHP Parse error: syntax error, unexpected token \"X\", expecting ..." and a file:line, halting before execution.
PHP Parse error: syntax error, unexpected token "readonly", expecting variable
in /home/runner/work/app/app/src/Money.php on line 7Common causes
Newer syntax on an older runner PHP
Code uses enum, readonly properties, or named arguments that the runner's older PHP cannot parse, even though it works locally.
A genuine syntax mistake
A missing brace, semicolon, or stray token makes the file unparseable on every PHP version.
How to fix it
Run the PHP the code targets
Provision the PHP version whose syntax the code uses so parsing succeeds.
- uses: shivammathur/setup-php@v2
with:
php-version: '8.2'Lint files to find the bad token
Use php -l to parse a file and report the exact syntax error without running it.
php -l src/Money.phpHow to prevent it
- Match the CI PHP version to the syntax the codebase uses.
- Add a
php -llint step over changed files as an early gate. - Test on the minimum PHP version you intend to support.