Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See