1.2.759. Nested Ternary

Ternary operators should not be nested too deep.

They are a convenient instruction to apply some condition, and avoid a if() structure. It works best when it is simple, like in a one liner.

However, ternary operators tends to make the syntax very difficult to read when they are nested. It is then recommended to use an if() structure, and make the whole code readable. This is a separate analysis from PHP’s preventing nested ternaries without parenthesis.

<?php

// Simple ternary expression
echo ($a == 1 ? $b : $c) ;

// Nested ternary expressions
echo ($a === 1 ? $d === 2 ? $b : $d : $d === 3 ? $e : $c) ;
echo ($a === 1 ? $d === 2 ? $f ===4 ? $g : $h : $d : $d === 3 ? $e : $i === 5 ? $j : $k) ;

//Previous expressions, written as a if / Then expression
if ($a === 1) {
    if ($d === 2) {
        echo $b;
    } else {
        echo $d;
    }
} else {
    if ($d === 3) {
        echo $e;
    } else {
        echo $c;
    }
}

if ($a === 1) {
    if ($d === 2) {
        if ($f === 4) {
            echo $g;
        } else {
            echo $h;
        }
    } else {
        echo $d;
    }
} else {
    if ($d === 3) {
        echo $e;
    } else {
        if ($i === 5) {
            echo $j;
        } else {
            echo $k;
        }
    }
}

?>

Name

Default

Type

Description

minNestedTernary

2

integer

Minimal number of nested ternary to report.

See also Nested Ternaries are Great and Nested Ternary Without Parenthesis.

1.2.759.1. Suggestions

  • Replace ternaries by if/then structures.

  • Replace ternaries by a functioncall : this provides more readability, offset the actual code, and gives room for making it different.

1.2.759.2. Specs

Short name

Structures/NestedTernary

Rulesets

All, Analyze, CE, CI-checks

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

ternary

ClearPHP

no-nested-ternary

Examples

SPIP, Zencart

Available in

Entreprise Edition, Community Edition, Exakat Cloud