1.2.760. Nested Ternary Without Parenthesis

It is not allowed to nest ternary operator within itself, without parenthesis. This has been implemented in PHP 7.4.

The reason behind this feature is to keep the code expressive. See the Warning message for more explanations

<?php

$a ? 1 : ($b ? 2 : 3);

// Still valid, as not ambiguous
$a ? $b ? 1 : 2 : 3;

// Produces a warning
//Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`
$a ? 1 : $b ? 2 : 3;

?>

See also PHP RFC: Deprecate left-associative ternary operator.

1.2.760.1. Suggestions

  • Add parenthesis to nested ternary calls

1.2.760.2. Specs

Short name

Php/NestedTernaryWithoutParenthesis

Rulesets

All, Appinfo, CE, Changed Behavior, CompatibilityPHP74, Deprecated

Exakat since

1.9.4

PHP Version

With PHP 7.4 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

ternary, parenthesis

Available in

Entreprise Edition, Community Edition, Exakat Cloud