Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP rejects a chained ternary that has no parentheses

ErrorUnparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`

Short answer

Before PHP 8.0 a chained ternary was evaluated left to right, unlike most languages. PHP 7.4 deprecated relying on that and PHP 8.0 requires parentheses. Add them to say which grouping you meant, or rewrite the chain with match, ?? or a ?: chain.

  • Applies to PHP 7.4, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Lint every file so PHP names each line that still has an unparenthesized chain.

    php -v
    find . -name '*.php' -not -path './vendor/*' -print0 | xargs -0 -n1 php -l | grep -v "No syntax errors"
  2. 2Match what you see, then jump to its fix

    1. 1The message appears on a line like $x = $a ? 'one' : $b ? 'two' : 'three';Old code relied on left-to-right evaluationFix
    2. 2Every branch of the chain is the value itself: $a ? $a : ($b ? $b : $c).You want the first non-empty value, not nested logicFix
    3. 3Several branches compare the same variable: $s == 1 ? 'a' : $s == 2 ? 'b' : 'c'.The chain maps one value to several outputsFix

What does the error look like?

  • Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`
  • Nested ternary expressions without parentheses fail to compile on PHP 8

Cause 1 of 3

Old code relied on left-to-right evaluation

1Check

The PHP manual says that before 8.0 ternaries were left-associative, that relying on it was deprecated in 7.4, and that in 8.0 the operator is non-associative. The 8.0 migration guide adds that nested ternaries now need explicit parentheses.

grep -rn -E "\? .* : .* \? " --include=*.php . | grep -v vendor/

2Fix

Add parentheses for the grouping you intend. The right-nested form is what most people expect from a chain; the left form keeps the old PHP 7 meaning, so choose by reading what the code should do.

// what most code means: if A then one, else if B then two, else three
$x = $a ? 'one' : ($b ? 'two' : 'three');

// the old PHP 7 meaning
$x = ($a ? 'one' : $b) ? 'two' : 'three';

3Verify

Lint the file and run the page or test that uses it.

php -l path/to/file.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

You want the first non-empty value, not nested logic

1Check

The manual says chaining short ternaries (?:) is stable and returns the first argument that evaluates to a non-falsy value.

grep -rn -E "\? \$[a-z_]+ :" --include=*.php . | grep -v vendor/

2Fix

Use the short ternary chain for first-truthy, or ?? when you only mean not-null. Note that ?? ignores undefined-index warnings, while ?: still warns on undefined values.

$name = $input ?: $default ?: 'Anonymous';
$page = $_GET['page'] ?? 1;

3Verify

Run the code with the first value empty and then set.

php -r '$a=0; $b=""; echo $a ?: $b ?: "fallback";'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The chain maps one value to several outputs

1Check

A match expression (PHP 8.0 and later) compares with identity (===) and returns a value; it throws UnhandledMatchError when no arm matches and there is no default.

php -r 'var_dump(PHP_VERSION_ID >= 80000);'

2Fix

Rewrite as match, with a default arm. Check the types first: match is strict, so the string '1' does not match the integer 1, unlike ==.

$label = match ($status) {
    1 => 'active',
    2 => 'paused',
    default => 'unknown',
};

3Verify

Run it with each value, including one that falls to default.

php -l path/to/file.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Parenthesize every nested ternary, even where PHP does not force you to.
  • Run php -l over the whole project in CI before moving to a new PHP version.
  • Prefer match for mapping a value to a result.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.