Parse error on a | in a parameter type: the PHP version is too old
Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE)Short answer
A | between two types, like int|string, is a union type, added in PHP 8.0.0. A parser older than that stops at the pipe and reports an unexpected '|'. So the code is fine and the PHP is too old. Check which PHP the web server and the command line each run, then upgrade the one that is behind.
Fix it fast
1Find out which cause you have
Ask PHP for its version on the command line, then ask the web server the same thing with a one-line script.
php -v # then open this file in the browser <?php echo PHP_VERSION, ' ', php_ini_loaded_file();2Match what you see, then jump to its fix
What does the error look like?
Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE)
Cause 1 of 2
The code uses union types and the PHP version is below 8.0
1Check
Union types were added in PHP 8.0.0. The older ?Type form, which means Type or null, works from 7.1.0, so a pipe between two types is the clue.
grep -rn -E "function .*\(.*[a-zA-Z]\|[a-zA-Z]+ \$" --include=*.php src/2Fix
Run PHP 8.0 or newer. If you cannot upgrade yet, rewrite the type as a docblock and drop the union from the signature.
// needs PHP 8.0+
function find(int|string $id): ?array {}
// works on PHP 7.1+
/** @param int|string $id */
function find($id): ?array {}3Verify
Lint the file with the PHP version that will run it.
php -l src/Repository.phpConfirmed by php.net
Cause 2 of 2
The web server runs an older PHP than the command line
1Check
On WAMP and similar stacks, Apache loads its own PHP build. phpinfo-style output from a web request shows the version and the php.ini that web PHP loaded.
<?php
echo PHP_VERSION, "\n";
echo php_ini_loaded_file();2Fix
Switch the web server to the newer PHP version in your stack's control panel, restart Apache, and point the PATH used by the command line at the same build.
php --ini3Verify
Reload the one-line script in the browser and confirm it prints 8.0 or higher.
php -r 'echo PHP_VERSION;'Confirmed by php.net
How do I stop it coming back?
- Set a minimum version in composer.json (the php entry under require) so a project states what it needs.
- Keep development, CI and production on the same PHP major version.
- Check release notes of a framework before upgrading it, since new releases raise the PHP floor.
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 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 fix