Maaz Zindani

Loading the index…

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

Parse error on a | in a parameter type: the PHP version is too old

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

  • Applies to PHP 7.4, PHP 8.3, WAMP, Windows 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The error line has a type such as int|string or array|null in a function signature or propertyThe code uses union types and the PHP version is below 8.0Fix
    2. 2php -v shows 8.x but the browser still shows the parse errorThe web server runs an older PHP than the command lineFix

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

Confirmed by php.net

Did this fix it?

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

3Verify

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

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