PHP Fatal error: Unparenthesized a ? b : c ? d : e
is not supported. Use either (a ? b : c) ? d : e
or a ? b : (c ? d : e)
The fault is the programmer for not using a switch statement.
âphp doesnât stop me from coding like a moron, therefore php sucksâ
How about âphp enables me to code like a moronâ, or even better, "php breaks common conventions and forces me to think about every little detail and special edge case, slowing me down if I donât want to accidentally âcode like a moronâ "
Nested ternary operators emerge because of the lack of if/switch expressions (which is C fault), so they are âusefulâ (they shouldnât be). However, PHP is the only language that treats it as left associative. This has 2 problems:
- You are forced to use parenthesis. Some (insane) people might do: (cond1) ? âAâ : (cond2) ? âBâ : âCâ And it makes sense. Its ugly af, but it makes sense. But PHP now forces you to use more parethesis. Itâs making you work more.
- It breaks convention. If you come from any other language and use ternary operators, you will get unexpected results. After hours of banging your head against the wall, you realize the problem. And now you have to learn a new edge case in the language, and what to do to actually use the language.
âBut you shouldnât use ternary operators anyway! Use if/switch/polymorphic dispatch/goto/anything elseâ
True, but still, the feature is there, and its bad. The fact that there are other alternatives doesnât make the PHP ternary operator worse than other languagesâ ternary operator.
PHP works against you. Thatâs the problem. The ternary operator is not a good example, since there are alternatives. But look at something so simple, so mundane like strpos.
If strpos doesnât find returns false
. Every other language returns -1. And if you then use this value elsewhere, PHP will cast it to 0 for you. Boom, your program is broken, and you have to stare at the screen for hours, looking for the error.
âBuT yOU sHoUlD AlwAyS cHEcK tHe rETurN eRRor!â
And even if thatâs true, if we all must check the return value, does PHP force you to do so? Like checked exceptions in Java? Or all the Option
& Result
in Rust? throws, throws, throws⌠unwrap, unwrap, unwrap⌠(Many) people hate those features
PHP works against you. And thatâs why its bad.
I say that php breaks math entirely, and is therefore bad. ââ == null returns true null == [] returns true ââ == [] returns false.
In more recent versions it gets worse, because it has 0 == âany textâ return true, âany textâ == true return true, and 1 == true return true So indirectly 1 = 0, and now math is more directly broken.
I just tested these out out of curiosity.
0==âtextâ returns false in PHP 8.2 as Iâd expect.
The others make sense in the way that php juggles between types. An empty variable can type-juggle to null, but an array canât be directly compared with a string.
(Although you wouldnât really want to compare an array with a string, PHP just treats an array as greater than other variables. So weirdly, ([] > ââ) == true.)
If youâre trying to directly compare different variable types in any language without strong typing, youâre going to have edge-case results which you might not expect.
My âcoding like a moronâ message still stands. PHP isnât a strongly typed language and it doesnât tell you off for trying stupid stuff like comparing a string with an int. Nor do other languages like JavaScript.
[This comment has been deleted by an automated system]
This is not valid syntax as of 2020. PHP 8 fixed a lot of issue like this as well as a lot of function and variable type issues.
Also this was deprecated in PHP 7 (2015).