# [RFC] Deprecate left-associative ternary operator

- Thread: https://externals.io/message/105163
- Original archive: https://news-web.php.net/php.internals/105163
- Started by Nikita Popov on 2019-04-09T09:54:37+00:00
- Messages: 23
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #105163 — Nikita Popov — 2019-04-09T09:54:37+00:00

URL: https://externals.io/message/105163#105163

Hi internals,

Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
deprecation and removal of the left-associative behavior of ternaries.
Instead, explicit parentheses should be used:

https://wiki.php.net/rfc/ternary_associativity

This RFC makes nested ternaries without disambiguating parentheses an error
in PHP 8 -- we might want to consider making them right-associative
instead, which is both useful and matches the behavior of other languages.

Regards,
Nikita

---

## #105164 — Bishop Bettini — 2019-04-09T10:19:11+00:00

URL: https://externals.io/message/105163#105164

>
> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
>
> https://wiki.php.net/rfc/ternary_associativity
>
> This RFC makes nested ternaries without disambiguating parentheses an error
> in PHP 8 -- we might want to consider making them right-associative
> instead, which is both useful and matches the behavior of other languages.
>

I'd support mandatory disambiguation, though I feel we should go all in and
introduce a ternary with right associativity.

See also Bug 61915[1], which suggests leaving ? left and introducing ?? for
right. Personally I don't see any benefit to keeping a ternary with left
associativity.

[1]: https://bugs.php.net/bug.php?id=61915

---

## #105166 — Rowan Collins — 2019-04-09T10:24:05+00:00

URL: https://externals.io/message/105163#105166

> Hi internals,
>
> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
>
> https://wiki.php.net/rfc/ternary_associativity
>


Thanks for putting this forward, it removes one more WTF from the language.
I think non-associativity is safest, because even if it's rarely desirable,
code may be out there that works accidentally with the current situation.
There's also the risk that someone would get used to it working one way in
PHP 8, and accidentally use it in code that's still deployed on PHP 7.

In both cases, it's better to generate an error and force users to be
explicit than introduce subtle bugs. We can then look at making it
right-associative in PHP 9, because anybody that needs to support 7, 8 and
9 can be explicit, and hopefully not many people will be jumping directly
between 7 and 9.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #105172 — Gabriel O — 2019-04-09T11:36:35+00:00

URL: https://externals.io/message/105163#105172

Hello, I am interested to know if this affects behaviour of following code:

putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize);

> 
> 
> Hi internals,
> 
> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
> 
> https://wiki.php.net/rfc/ternary_associativity
> 
> This RFC makes nested ternaries without disambiguating parentheses an error
> in PHP 8 -- we might want to consider making them right-associative
> instead, which is both useful and matches the behavior of other languages.
> 
> Regards,
> Nikita

---

## #105175 — Rowan Collins — 2019-04-09T13:54:49+00:00

URL: https://externals.io/message/105163#105175

> See also Bug 61915[1], which suggests leaving ? left and introducing ?? for
> right. Personally I don't see any benefit to keeping a ternary with left
> associativity.
>


?? wouldn't work, because that's now the null-coalescing operator. (I've no
idea if it would be ambiguous to the parser to have it mean both, but it
would be horrible for humans to read!)

Obviously, that doesn't rule out having a new syntax of some sort, but it
highlights the usual problem with suggesting new operators: we're running
out of punctuation!

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #105219 — Stanislav Malyshev — 2019-04-10T19:21:57+00:00

URL: https://externals.io/message/105163#105219

Hi!

> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
> 
> https://wiki.php.net/rfc/ternary_associativity

Please, let's not mess with language syntax and require people to
rewrite their code just because suddenly after 20 years of existence in
the language we feel maybe the precedence should be different. I
literally can't think of a single case where it would improve anyone's
life in any substantial way. If you wanted to use parentheses, you can
do it now - you can even have it enforced by code analyzer if you wish
so, in your private code base. But I can easily imagine dealing with
tons of deprecation warnings (which usually break CI unit tests and
wreak general havoc) or even worse, useless errors, in existing code,
which would become broken for no reason but... honestly, I don't even
know how to finish this phrase, I can't even think of a single reason I
could explain to anybody why their code would no longer work in 8.0.

I understand 8.0 is a good target for breaking things. But that doesn't
mean we have to do it as much as possible.

And this absolutely doesn't mean we need to get 7.4 to break things (and
yes, deprecation warning is breaking things, in most cases).
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #105220 — Michael Wallner — 2019-04-10T20:10:52+00:00

URL: https://externals.io/message/105163#105220

Nikita Popov <nikita.ppv@gmail.com> schrieb am Di., 9. Apr. 2019, 11:54:

> Hi internals,
>
> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
>
> https://wiki.php.net/rfc/ternary_associativity
>
> This RFC makes nested ternaries without disambiguating parentheses an error
> in PHP 8 -- we might want to consider making them right-associative
> instead, which is both useful and matches the behavior of other languages.
>
> Regards,
> Nikita
>

Simple, +1.

Regards,
Mike

---

## #105221 — Levi Morrison — 2019-04-10T20:33:44+00:00

URL: https://externals.io/message/105163#105221

>
> Hi!
>
> > […]
>
> Please, let's not mess with language syntax and require people to
> rewrite their code just because suddenly after 20 years of existence in
> the language we feel maybe the precedence should be different. I
> literally can't think of a single case where it would improve anyone's
> life in any substantial way. If you wanted to use parentheses, you can
> do it now - you can even have it enforced by code analyzer if you wish
> so, in your private code base. But I can easily imagine dealing with
> tons of deprecation warnings (which usually break CI unit tests and
> wreak general havoc) or even worse, useless errors, in existing code,
> which would become broken for no reason but... honestly, I don't even
> know how to finish this phrase, I can't even think of a single reason I
> could explain to anybody why their code would no longer work in 8.0.
>
> I understand 8.0 is a good target for breaking things. But that doesn't
> mean we have to do it as much as possible.
>
> And this absolutely doesn't mean we need to get 7.4 to break things (and
> yes, deprecation warning is breaking things, in most cases).
> --
> Stas Malyshev
> smalyshev@gmail.com
>
> --
> 
> 

We do not consider new warnings, including deprecation notices, as
breaking changes. We know that due to error handlers it can have an
effect, but it's just part of the process.

I disagree with Stas on the value. The precedence is wrong, and I know
of a few codebases which had bugs due to this. In every case, the fix
was to avoid it altogether. I agree with statement from the RFC:

> As left-associative ternaries are almost certainly bugs, the impact of this change should be minimal.

Thanks, Nikita, on putting this together. It's definitely not a "fun" RFC.

---

## #105222 — Claude Pache — 2019-04-10T20:42:21+00:00

URL: https://externals.io/message/105163#105222

> Le 10 avr. 2019 à 21:21, Stanislav Malyshev <smalyshev@gmail.com> a écrit :
> 
> Hi!
> 
> > […]
> 
> Please, let's not mess with language syntax and require people to
> rewrite their code just because suddenly after 20 years of existence in
> the language we feel maybe the precedence should be different. I
> literally can't think of a single case where it would improve anyone's
> life in any substantial way.

I bet that more than one developer has once written <?php cond1 ? value1 : cond2 ? value2 : value3 ?>, and later has wasted a nontrivial amount of time in order to understand why they got some wrong result. So, definitely, making that trap a syntax error is a real tangible improvement of the language. 

> If you wanted to use parentheses, you can
> do it now - you can even have it enforced by code analyzer if you wish
> so, in your private code base.

A language that requires their users to use a code analyser (or to read the manual thoroughly before starting to code) in order to keep their sanity, well, ... has a problem. 
 
> But I can easily imagine dealing with
> tons of deprecation warnings (which usually break CI unit tests and
> wreak general havoc) or even worse, useless errors, in existing code,
> which would become broken for no reason

As I can’t imagine a case where someone would have taken advantage of the current left-associativity semantics, I can’t imagine either someone dealing with “a ton of deprecation warnings” because of that. But TBH, my imagination is sometimes very selective. However, what I can imagine, is that hidden bugs will be revealed as a side-effect of those deprecation warnings, and time will be taken to understand and correct them. 

—Claude

> but... honestly, I don't even
> know how to finish this phrase, I can't even think of a single reason I
> could explain to anybody why their code would no longer work in 8.0.
> 
> I understand 8.0 is a good target for breaking things. But that doesn't
> mean we have to do it as much as possible.
> 
> And this absolutely doesn't mean we need to get 7.4 to break things (and
> yes, deprecation warning is breaking things, in most cases).
> -- 
> Stas Malyshev
> smalyshev@gmail.com

---

## #105223 — bjorn.x.larsson@telia.com — 2019-04-10T20:44:59+00:00

URL: https://externals.io/message/105163#105223

Den 2019-04-09 kl. 11:54, skrev Nikita Popov:
> Hi internals,
>
> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
>
> https://wiki.php.net/rfc/ternary_associativity
>
> This RFC makes nested ternaries without disambiguating parentheses an error
> in PHP 8 -- we might want to consider making them right-associative
> instead, which is both useful and matches the behavior of other languages.
>
> Regards,
> Nikita
Thanks for bringing this up. I recall it was up for discussion in
PHP 7.0, never made it then. Hope it does it now!

Should one in the RFC reference this bug report from 2012?
- https://bugs.php.net/bug.php?id=61915

r//Björn L

---

## #105269 — lauri.kentta@gmail.com — 2019-04-13T22:12:36+00:00

URL: https://externals.io/message/105163#105269

> Inspired by Bob's recent RFC for concat precedence, I'd like to propose 
> a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used: ...

What about nesting in the middle part: 1 ? 2 ? 3 : 4 : 5
This is unambiguous and should be allowed IMO.

-- 
Lauri Kenttä

---

## #105307 — Nikita Popov — 2019-04-17T10:21:04+00:00

URL: https://externals.io/message/105163#105307

On Sun, Apr 14, 2019 at 12:12 AM Lauri Kenttä <lauri.kentta@gmail.com>
wrote:

> 
> > […]
>
> What about nesting in the middle part: 1 ? 2 ? 3 : 4 : 5
> This is unambiguous and should be allowed IMO.
>

This case is not affected, as it's not related to associativity. I've
mentioned this in the RFC explicitly now.

Nikita

---

## #105328 — Nikita Popov — 2019-04-22T08:09:44+00:00

URL: https://externals.io/message/105163#105328

> Hi internals,
> The only objection here came from Gabriel, and I don't think we'll come to
> an agreement.
> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
>
> https://wiki.php.net/rfc/ternary_The only objection here came from
> Gabriel, and I don't think we'll come to an agreement.associativity
> <https://wiki.php.net/rfc/ternary_associativity>
>
> This RFC makes nested ternaries without disambiguating parentheses an
> error in PHP 8 -- we might want to consider making them right-associative
> instead, which is both useful and matches the behavior of other languages.
>
> Regards,
> Nikita
>

Heads up: Plan to start voting tomorrow.

Apart from Bishop, would anyone else prefer to directly go to right
associativity in PHP 8 rather than making it an error? I'm rather partial
to that myself, maybe it can be included as a voting option...

Nikita

---

## #105330 — Sara Golemon — 2019-04-22T15:28:09+00:00

URL: https://externals.io/message/105163#105330

> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
> deprecation and removal of the left-associative behavior of ternaries.
> Instead, explicit parentheses should be used:
>
> https://wiki.php.net/rfc/ternary_associativity
>
> This RFC makes nested ternaries without disambiguating parentheses an error
> in PHP 8 -- we might want to consider making them right-associative
> instead, which is both useful and matches the behavior of other languages.
>
> Sorry, but I'm a "No" on this one.

It'd be swell if PHP's ternary matched other languages, but it doesn't, and
we have 20 years of code in the wild which has accepted that fact and moved
on.  I don't see the benefit in breaking that code. Advise the use of
parentheses and move on.

-Sara

---

## #105331 — Stanislav Malyshev — 2019-04-22T17:42:50+00:00

URL: https://externals.io/message/105163#105331

Hi!

> It'd be swell if PHP's ternary matched other languages, but it doesn't, and
> we have 20 years of code in the wild which has accepted that fact and moved
> on.  I don't see the benefit in breaking that code. Advise the use of
> parentheses and move on.

Exactly.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #105346 — Nikita Popov — 2019-04-23T08:56:32+00:00

URL: https://externals.io/message/105163#105346

> 
>
> > […]
>
> It'd be swell if PHP's ternary matched other languages, but it doesn't,
> and we have 20 years of code in the wild which has accepted that fact and
> moved on.  I don't see the benefit in breaking that code. Advise the use of
> parentheses and move on.
>

Can't say I understand your position here. Don't want to change the ternary
from left-associative to right-associative? I can understand that: Silent
behavior changes are always problematic. This is not what the RFC proposes
though.

20 years of code in the wild has not "accepted that fact and moved on". If
a left-associative ternary is used, it is almost certainly a bug. If people
use this structure by accident (because it is familiar from other
programming languages), I'd like them to get an error instead of having to
figure out why their obviously correct code is not working or, in the worse
case, just leave behind buggy code.

Nikita

---

## #105366 — bjorn.x.larsson@telia.com — 2019-04-24T09:10:53+00:00

URL: https://externals.io/message/105163#105366

Den 2019-04-22 kl. 10:09, skrev Nikita Popov:
> 
>
> > […]
> Heads up: Plan to start voting tomorrow.
>
> Apart from Bishop, would anyone else prefer to directly go to right
> associativity in PHP 8 rather than making it an error? I'm rather partial
> to that myself, maybe it can be included as a voting option...
>
> Nikita

Hi,

I think one should go for the right associativity. 8.x is the right place to
do it, if at all. Doing it directly in 8.0 has the benefit that when 
upgrading
to a major version it justifies an upgrading project for an application and
one also might get tool support, e.g. the php7mar tool for 7.0 was quite
valuable. Also IDEs like phpstorm has good support.

One then has to weigh legacy code versus new code being written with a
behaviour different from almost all other languages. I'm not sure if this
is an appropriate example to compare with, but I came to think on the
PHP 7.0 Uniform Variable Syntax rfc regarding migration effort and BC
impact.

r//Björn L

---

## #105370 — Claude Pache — 2019-04-24T13:11:43+00:00

URL: https://externals.io/message/105163#105370

> Le 24 avr. 2019 à 11:10, Björn Larsson <bjorn.x.larsson@telia.com> a écrit :
> 
> Den 2019-04-22 kl. 10:09, skrev Nikita Popov:
> > […]
> 
> Hi,
> 
> I think one should go for the right associativity. 8.x is the right place to
> do it, if at all. Doing it directly in 8.0 has the benefit that when upgrading
> to a major version it justifies an upgrading project for an application and
> one also might get tool support, e.g. the php7mar tool for 7.0 was quite
> valuable. Also IDEs like phpstorm has good support.
> 
> One then has to weigh legacy code versus new code being written with a
> behaviour different from almost all other languages. I'm not sure if this
> is an appropriate example to compare with, but I came to think on the
> PHP 7.0 Uniform Variable Syntax rfc regarding migration effort and BC
> impact.
> 
> r//Björn L
> 
> -- 
> 
> 
> 

If you go for right-associative straight in PHP 8, you make it mandatory to use a static analysis tool for:

* migrating code from one major version to the next;
* maintaining a library that aims to run in two major consecutive versions.

Although that might be a no-brainer for anyone following internals, I doubt it is the case for anyone writing PHP.

With a more step-wise approach (left-associative in PHP 7, non-associative in PHP 8, right-associative in PHP 9 or 10), the situation is much safer and simpler: you can’t even compile the code in at least one of the consecutive major versions you aim to support, and the error message says exactly what and where you have to fix in order to make it working.

As says an Italian proverb: chi va piano, va sano e lontano.

—Claude

---

## #105397 — Sara Golemon — 2019-04-24T17:25:06+00:00

URL: https://externals.io/message/105163#105397

> Can't say I understand your position here. Don't want to change the
> ternary from left-associative to right-associative? I can understand that:
> Silent behavior changes are always problematic. This is not what the RFC
> proposes though.
>
> Did the RFC change since introduction? I may have been looking at a stale
load of the page (opened it up awhile ago and only just got around to
responding).

As for what it says atm, I still think it goes too far.  the warning in 7.4
I can support, but I'm not a fan of breaking existing code on a feature
that's this old. Ratchet up the warning in 8.0 if you'd like, but error is
further than I'm willing to go on this one.


> 20 years of code in the wild has not "accepted that fact and moved on". If
> a left-associative ternary is used, it is almost certainly a bug. If people
> use this structure by accident (because it is familiar from other
> programming languages), I'd like them to get an error instead of having to
> figure out why their obviously correct code is not working or, in the worse
> case, just leave behind buggy code.
>
> I'm on dismal wifi at the moment, else I'd do some searches, but do you
have any examples of code in the wild subject to this bug?
I agree it's a lousy state, but it's not emergent.

-Sara

---

## #105407 — M. W. Moe — 2019-04-24T18:56:31+00:00

URL: https://externals.io/message/105163#105407

Hello,

the underlaying discussion here is more important; than just voting yes or
no on uncompleted hamiltonian graphs; the real question here does php8 will
be a break thru; meaning bugger off the mistakes of the past or a
continuation of them; until this RCF does not happen; I would say nothing
interesting will ever happen.

Have a good day.



> 
>
> > […]
> that:
> > […]
> load of the page (opened it up awhile ago and only just got around to
> responding).
>
> As for what it says atm, I still think it goes too far.  the warning in 7.4
> I can support, but I'm not a fan of breaking existing code on a feature
> that's this old. Ratchet up the warning in 8.0 if you'd like, but error is
> further than I'm willing to go on this one.
>
>
> > […]
> If
> > […]
> people
> > […]
> to
> > […]
> worse
> > […]
> have any examples of code in the wild subject to this bug?
> I agree it's a lousy state, but it's not emergent.
>
> -Sara

---

## #105421 — Sara Golemon — 2019-04-24T22:50:43+00:00

URL: https://externals.io/message/105163#105421

> until this RCF does not happen; I would say nothing interesting will ever
> happen.
>
> Please, Reality TV is less hyperbolic than that.
JIT isn't interesting?
Typed Properties isn't interesting?
FFI isn't interesting?
 Preload isn't interesting?

Pack it up, folks!  Nothing we're doing here is interesting or ever will
be, because the associativity of the ternary operator is wrong.

-Sara

---

## #105424 — Christoph M. Becker — 2019-04-24T23:48:38+00:00

URL: https://externals.io/message/105163#105424

> 
>
> > […]
>
> I'm on dismal wifi at the moment, else I'd do some searches, but do you
> have any examples of code in the wild subject to this bug?
> I agree it's a lousy state, but it's not emergent.

See <https://externals.io/message/105345#105389>.

--
Christoph M. Becker

---

## #105429 — Sara Golemon — 2019-04-25T00:27:11+00:00

URL: https://externals.io/message/105163#105429

On Wed, Apr 24, 2019 at 6:48 PM Christoph M. Becker <cmbecker69@gmx.de>
wrote:

> 
>
> > […]
> wrote:
> > […]
> If
> > […]
> people
> > […]
> to
> > […]
> worse
> > […]
>
> See <https://externals.io/message/105345#105389>.
>
>  Okay, so the most compelling part for me in this analysis is the fact
that Symfony and ZF have issues.  I can shrug off a lot of questionable PHP
coders, but i have to acknowledge major frameworks stumbling on this gotcha.

Convinced.
+1

-Sara
