Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I used to write php and JavaScript at the same time. Turns out nested ternaries get evaluated differently in both languages, leading to some subtle bugs sometimes. I will never write those without parentheses again.


iirc php is the only (popular) language with messed up ternary-if associativity, it works as you'd expect pretty much everywhere else


I believe you're correct; I'm not aware of any other language with a left-associative ternary.

This will be deprecated as of PHP 7.4[0], and will cause a compile error in PHP 8.0.

However, instead of switching straight to right-associativity, PHP will require explicit parentheses to disambiguate nested ternaries.

[0] https://wiki.php.net/rfc/ternary_associativity




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: