Maaz Zindani

Loading the index…

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

PHP says unexpected token '?' on code that looks correct

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

  • Applies to PHP 8.3, VS Code, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1The line contains $a?->b and php -v prints 7.xThe nullsafe operator ?-> is running on PHP older than 8.0Fix
    2. 2The line contains ??= and the version is 7.3 or lowerThe ??= operator is running on PHP older than 7.4Fix
    3. 3The error points at a function signature such as function f(?string $x): ?intNullable types like ?string on PHP older than 7.1Fix
    4. 4The terminal runs the code fine but VS Code underlines it with PHP2014The editor's linter uses a different PHP than your terminalFix

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/ | head

2Fix

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

Confirmed by php.net

Did this fix it?

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 -v

2Fix

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

Confirmed by php.net

Did this fix it?

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 -v

2Fix

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

Confirmed by php.net

Did this fix it?

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 Path

2Fix

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 -v

Confirmed by code.visualstudio.com

Did this fix it?

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

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 fix

Back 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

Next step

Want this handled for good?

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