syntax error, unexpected ‘|’, expecting variable (T_VARIABLE)

Description

Union types were introduced in PHP 8.0: union type allows the specification of several distinct types, separated by a | pipe character. Until PHP 8.0, types were immediately followed by the variable or property name.

Example

<?php

class X {
    private int|float $property;
}

function foo(int|float $property) {}


?>

Solutions

  • Upgrade to PHP 8.0.

  • Find a common supertype to the two (or more) involved types.

  • Remove all types.