Take the State of PHP 2026 Survey →

Deprecate left-associative ternary operator View as Markdown

Implemented (PHP 7.4)
Authors
Nikita Popov nikic@php.net
Date
2019-04-09
Target
PHP 7.4 and PHP 8.0
Implementation
https://github.com/php/php-src/pull/4017
Source
wiki.php.net/rfc/ternary_associativity

Vote

Deprecate and remove left-associative ternary without explicit parentheses? closed
Yes
35
No
10
45 voters
andrey Yes 2019-04-23 08:46
ashnazg Yes 2019-04-24 16:13
bishop Yes 2019-04-24 16:46
bwoebi Yes 2019-04-23 21:49
carusogabriel Yes 2019-04-27 16:35
derick Yes 2019-04-23 09:31
didou Yes 2019-04-27 12:42
diegopires No 2019-04-23 12:11
duncan3dc Yes 2019-04-23 22:50
emir Yes 2019-04-29 12:11
galvao Yes 2019-04-26 19:54
girgias Yes 2019-04-23 11:34
guilhermeblanco Yes 2019-04-23 19:38
jasny Yes 2019-04-30 15:56
jbnahan Yes 2019-04-24 15:10
jhdxr Yes 2019-04-25 14:50
kalle No 2019-04-24 11:58
kelunik Yes 2019-04-28 15:58
kinncj No 2019-04-23 14:55
levim Yes 2019-04-24 14:30
malukenho Yes 2019-05-01 10:06
marcio Yes 2019-04-24 17:37
mariano Yes 2019-04-23 11:54
mbeccati Yes 2019-04-30 08:42
mcmic Yes 2019-04-23 13:31
mike Yes 2019-04-25 08:14
nikic Yes 2019-04-23 08:36
ocramius Yes 2019-04-23 09:10
patrickallaert Yes 2019-05-02 22:10
petk Yes 2019-05-03 18:02
pmmaga Yes 2019-05-06 10:15
pollita Yes 2019-04-25 00:27
rasmus No 2019-04-25 00:10
reywob No 2019-04-29 07:27
salathe No 2019-04-23 08:46
santiagolizardo Yes 2019-04-30 22:41
sebastian Yes 2019-04-24 05:20
sergey Yes 2019-04-24 07:59
stas No 2019-04-23 17:10
trowski Yes 2019-04-23 15:34
weierophinney Yes 2019-04-30 14:26
yohgaki Yes 2019-04-25 00:53
yunosh No 2019-04-24 08:46
zeev No 2019-04-23 09:10
zimt No 2019-04-29 06:42

Discussions on the mailing list

[RFC] Deprecate left-associative ternary operator
by Nikita Popov · 23 messages
[VOTE] Deprecate left-associative ternary
by Nikita Popov · 5 messages
Ternary associativity
by Nikita Popov · 8 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

Unlike most (all?) other languages, the ternary operator in PHP is left-associative rather than right-associative. The left-associative behavior is generally not useful and confusing for programmers who switch between different languages. This RFC proposes to deprecate and remove left-associativity for the ternary operator and require explicit use of parentheses instead.

As an example, the code

return $a == 1 ? 'one'
     : $a == 2 ? 'two'
     : $a == 3 ? 'three'
     : $a == 4 ? 'four'
               : 'other';

would in most (all?) other languages be interpreted as

return $a == 1 ? 'one'
     : ($a == 2 ? 'two'
     : ($a == 3 ? 'three'
     : ($a == 4 ? 'four'
               : 'other')));

which is both the useful and intuitive interpretation. In PHP, it is instead interpreted as

return ((($a == 1 ? 'one'
     : $a == 2) ? 'two'
     : $a == 3) ? 'three'
     : $a == 4) ? 'four'
               : 'other';

which is generally not what was intended.

Proposal

In PHP 7.4 using nested ternaries without explicit parentheses will throw a deprecation warning. In PHP 8.0 it will become a compile-time error instead.

1 ? 2 : 3 ? 4 : 5;   // deprecated
(1 ? 2 : 3) ? 4 : 5; // ok
1 ? 2 : (3 ? 4 : 5); // ok

This also applies when mixing short and long ternary syntax:

1 ?: 2 ? 3 : 4;   // deprecated
(1 ?: 2) ? 3 : 4; // ok
1 ?: (2 ? 3 : 4); // ok
 
1 ? 2 : 3 ?: 4;   // deprecated
(1 ? 2 : 3) ?: 4; // ok
1 ? 2 : (3 ?: 4); // ok

However, as an exception, explicit parenthesis are not required when combining two short ternaries:

1 ?: 2 ?: 3;   // ok
(1 ?: 2) ?: 3; // ok
1 ?: (2 ?: 3); // ok

The reason is that ($a ?: $b) ?: $c and $a ?: ($b ?: $c) will always yield the same result, even though the left-associative version will arrive at it in a less efficient manner.

Parentheses are also not required when nesting into the middle operand, as this is always unambiguous and not affected by associativity:

1 ? 2 ? 3 : 4 : 5 // ok
1 ? 2 ?: 3 : 4    // ok

The null-coalesce operator ?? is already right-associative and not affected by these changes.

Backward Incompatible Changes

Code exploiting left-associativity of the ternary operator will become a hard error in PHP 8. As left-associative ternaries are almost certainly bugs, the impact of this change should be minimal.

An analysis of the top 1000 composer packages found 12 instances that will be affected by this RFC. 9 of them are bugs, where a right-associative meaning was intended, but a left-associative one will be used. 3 uses of the form $a ? $b : $c ?: $d are potentially okay, because the difference between the two interpretations is small and it's not obvious to me which one is actually intended. Detailed analysis results are available at https://gist.github.com/nikic/b6214f87b0e4a7c6fe26919ac849194f.

Future Scope

We could make the ternary right-associative in a later release, after it has been an error for a while.

Vote

Voting started 2019-04-23 and ends 2019-05-07.

Vote: Deprecate and remove left-associative ternary without explicit parentheses? — Yes: 35 · No: 10 (closed)