PHP says unexpected token '?' on code that looks correct
PHP Parse error: syntax error, unexpected token "?"Short answer
The code is valid, but the PHP that parses it is too old. The nullsafe operator ?-> needs PHP 8.0, ??= needs 7.4 and ?type needs 7.1. Find the PHP version that actually runs the file, and check which one your editor's linter uses, because it may differ from your terminal.
Fix it fast
1Find out which cause you have
Ask the same PHP that reports the error for its version, and lint the file with it.
php -v php -l path/to/File.php2Match what you see, then jump to its fix
- 1
The line contains $a?->b and php -v prints 7.xThe nullsafe operator ?-> is running on PHP older than 8.0Fix - 2
The line contains ??= and the version is 7.3 or lowerThe ??= operator is running on PHP older than 7.4Fix - 3
The error points at a function signature such as function f(?string $x): ?intNullable types like ?string on PHP older than 7.1Fix - 4
The terminal runs the code fine but VS Code underlines it with PHP2014The editor's linter uses a different PHP than your terminalFix
- 1
What does the error look like?
PHP Parse error: syntax error, unexpected token "?"Syntax error: unexpected token '?' PHP(PHP2014)
Cause 1 of 4
The nullsafe operator ?-> is running on PHP older than 8.0
1Check
The PHP manual lists support for ?-> as new in PHP 8.0. Older versions cannot parse it.
php -v
grep -rn "?->" app/ | head2Fix
Run the code on PHP 8.0 or newer. If you cannot upgrade yet, rewrite the expression with a plain null check.
// needs PHP 8.0+
$city = $user?->address?->city;
// works on older PHP
$city = $user && $user->address ? $user->address->city : null;3Verify
Lint passes.
php -l app/Http/Controllers/UserController.phpConfirmed by php.net
Cause 2 of 4
The ??= operator is running on PHP older than 7.4
1Check
The null coalescing assignment operator ??= appears in the PHP 7.4 migration guide's new features.
php -v2Fix
Upgrade to 7.4 or newer, or use isset() on older PHP.
// PHP 7.4+
$data['key'] ??= 'default';
// older PHP
if (!isset($data['key'])) { $data['key'] = 'default'; }3Verify
Lint passes.
php -l path/to/File.phpConfirmed by php.net
Cause 3 of 4
Nullable types like ?string on PHP older than 7.1
1Check
Prefixing a type with a question mark was added in PHP 7.1.
php -v2Fix
Use PHP 7.1 or newer. Every supported PHP release has it.
function find(?string $name): ?int { return null; }3Verify
Lint passes.
php -l path/to/File.phpConfirmed by php.net
Cause 4 of 4
The editor's linter uses a different PHP than your terminal
1Check
VS Code lints with php -l using the executable in the php.validate.executablePath setting, or the first PHP on the system path.
which -a php
# VS Code: Settings > PHP > Validate: Executable Path2Fix
Point the setting at the PHP binary the project uses.
{
"php.validate.executablePath": "/usr/bin/php"
}3Verify
Reload the window; the squiggle disappears.
/usr/bin/php -vConfirmed by code.visualstudio.com
How do I stop it coming back?
- Declare the minimum PHP version in composer.json and keep the server on the same major version as development.
- Run php -l or your tests in CI with the production PHP version.
- Set the editor's PHP path per project, not once for the whole machine.
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 built on the same technologies:
Related fixes
Back end
PHP Parse error: syntax error, unexpected token
Parse error: syntax error, unexpected end of file
A parse error means PHP could not read the file, so nothing ran.
4 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fixBack end
CodeIgniter 3 loads the model but the property is undefined
The model name you are loading is the name of a resource that is already being used: user
CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.
2 causes, each with a fix