PHP rejects a chained ternary that has no parentheses
Unparenthesized `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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The message appears on a line like $x = $a ? 'one' : $b ? 'two' : 'three';Old code relied on left-to-right evaluationFix - 2
Every branch of the chain is the value itself: $a ? $a : ($b ? $b : $c).You want the first non-empty value, not nested logicFix - 3
Several branches compare the same variable: $s == 1 ? 'a' : $s == 2 ? 'b' : 'c'.The chain maps one value to several outputsFix
- 1
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.phpConfirmed by php.net
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
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.phpConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix