Unparenthesized a ? b : c ?: d is not supported.
Description
Mixing a full ternary with the short ternary operator (?:) requires a clear definition of priorities, by using parenthesis.
The code above could be understood as ($a ? $b : $c) ?: $d or $a ? $b : ($c ?: $d). Until PHP 8.0, the engine chose the first option, since the ternary operator was left-associative; since then, it must be explicitly written.
Note that, in the error message, the letters represent a full expression. They may be variables, as in the illustration, but any other expression.
Example
<?php
$x = $a ? $b : $c ?: $d;
?>
Alternatives
- Write the expression as
($a ? $b : $c) ?: $d. - Write the expression as
$a ? $b : ($c ?: $d). - Rewrite the expression as an if/then.
Related error messages
- unparenthesized-
a-?-b-:-c-?-d-:-e-is-not-supported. - unparenthesized-
a-?:-b-?-c-:-d-is-not-supported.
Changed Behavior
This error may appear following an evolution in behavior, in previous versions. See