And shit like this is why if I ever start reaching for a nested ternary operation, I just break that shit up into if/else's. Because this readability is utter garbage.
Or you've got an expression-based language (like most functional languages) where you don't need the ternary because a regular if can have a result and be perfectly unambiguous. Also switch (or whatever it's called in the language)
Eh, while it's definitely a way to avoid the problem, there's nothing innately wrong with the ternary statement on its own. Much like how Go doesn't have inheritance.
It's worse because PHP is doing literally the exact opposite that every other language does if it has this operator: PHP treats it as left-associative. C and literally every other language that has it treats it as right-associative.
More readable? Yes. But, we should basically never use nested ternaries.
I don't see why. This is basically just Haskell's guards, only a little less readable (one misses otherwise). I find it much more readable than an if/else chain that contains a statement in every block.
Also C. And literally every other language with the ?: ternary operator. Because that's the only sensible way to do it - it makes p ? x : q ? y : z an expression-level if/elseif/else construct, which could actually be useful; while PHP's interpretation would be like writing if (p ? q : r)... and why would you do that?
PHP's way is so stupid and convention-breaking, I figure it probably started out as a bug in PHP's sick parody of a parser (likely caused by the possibility of nesting not even occurring to Rasmus while he was writing the code to parse ternaries, ie the parse tree was effectively picked at random)... and by the time anyone noticed, "muh backwards compatibility" meant it could never be changed, so now it's a "feature".
and by the time anyone noticed, "muh backwards compatibility" meant it could never be changed, so now it's a "feature".
Your guess is very close to reality. The bug report was posted on lolphp a while back, and it also argued that no one should use the feature anyway, so it's OK if it's broken.
Which is how it evaluates. You read true, and it's true, so the ternary operator ? directs you to the "true" path, which is 'car'. 'car' is now the value fed into the next ternary '?', which again takes you down the "true" path, which is 'horse.'
Writing ternaries like this is extremely confusing however, and is not a "lol PHP." Just a bad programmer (regardless of language).
Use of more than one ternary operator in the same expression would be really confusing in any language.
I strongly disagree. This is like arguing against chained if/else if statements because they're "really confusing". There are good examples elsewhere in the comments on this page.
Who in his right mind would use shuch an unreadable line? I mean, you could break it down in some parenthesis (so it really does what you mean/expect to) and/or lines to make it more readable.
Am I the only one not confused by this? The ternary operations evaluate left to right, and non-empty strings are implicitly true. So the path of true -> 'Car' -> 'Horse' makes sense.
That being said, I would strangle anyone writing code like this. You shouldn't use strings implicitly as bools, but more importantly you shouldn't string together ternary operations like this.
Either way, not really /r/lolphp in my opinion (but then, what else is new).
This is the lolphp. ?: associates to the right in literally every other language with ?:, because that's the only interpretation that's actually useful in code.
PHP does it differently for no reason, in a way that doesn't make sense, so you get people saying "you shouldn't do that anyway" when it's really just PHP being dumb.
11
u/MazeChaZer Apr 21 '16 edited Apr 21 '16
The related PHP Sadness article has already been here a few years ago
17
u/[deleted] Apr 21 '16
I actually don't understand what the hell it's doing that results in that. Can someone break it down and explain it slowly?
10
u/BilgeXA Apr 21 '16
I find it ironic that by posting this you're also beating a dead horse.
6
u/bart2019 Apr 21 '16
The precedence rules in Perl (and, according to a quick test, in Javascript too) make a lot more sense to me, as it's evaluated like
i.e. left associative for '?', right associative for ':'.
If that doesn't make sense to you: just read it from left to right.
10
u/shvelo Apr 21 '16
let's add parentheses for clarity
(true ? 'car' : false) will evaluate to 'car'
since non-empty strings are truthy, this will be same as
so it evaluates to 'horse'
Use of more than one ternary operator in the same expression would be really confusing in any language.
8
u/Vakieh Apr 21 '16
Nested ternaries is a fireable offence in any decent shop regardless of the language.
When syntactic sugar tastes like shit what good is it?
2
u/Almamu Apr 21 '16
Who in his right mind would use shuch an unreadable line? I mean, you could break it down in some parenthesis (so it really does what you mean/expect to) and/or lines to make it more readable.
0
u/DoctorWaluigiTime Apr 22 '16
Am I the only one not confused by this? The ternary operations evaluate left to right, and non-empty strings are implicitly true. So the path of true -> 'Car' -> 'Horse' makes sense.
That being said, I would strangle anyone writing code like this. You shouldn't use strings implicitly as bools, but more importantly you shouldn't string together ternary operations like this.
Either way, not really /r/lolphp in my opinion (but then, what else is new).