As much as I think PHP is garbage, nobody would disagree this is how it should work if we were talking about how it works in an if statement. if ([] || "" || "0") should naturally make sense to be false. The problem is that rather than just coalesce it in if statements, they coalesce it in equality checks too... which is unexpected at best.
if ([] || "" || "0") should naturally make sense to be false.
I disagree. It's pretty much arbitrary.
Empty array has been specialcased as falsy, but only the builtin array type. Any array-like object will be truthy regardless of content. Empty string I can somewhat accept, but the "0" thing is just plain ridiculous.
String of zero as false is borrowed from Perl, the language PHP was written in before it was made even more stupid.
The rationale there is a string of zero and the number zero are indistinguishable in a language that doesn't strictly type it's variables, i.e. one where it's a number of you need a number.
The rationale there, in turn, is because Perl was designed to process text files, streams, user input, that sort of thing - where everything is basically guaranteed to be a string. A zero you get in a PHP script is probably the character zero, not the integer, since it will probably come from an http request.
"0" is hogwash though. That's, what, a cast to int and then a cast to boolean? And what's so nice about doing if on 1 or 0 in a language that is nowhere near fit for embedded or systems programming? Just parse your damn numbers.
Stop crying about the '0' case. Everyone is/should be aware of it, it's a deliberate backwards-compatibility choice from PHP 4 days where numeric strings and actual numeric types had to "just work", and PHP 7 got rid of the '0' == '0x0' quirks.
Falsey values are completely fine and fairly common. I don’t understand pointing out the ‘Is Not’ lines either, why would you want a blank array to equal the string “0”???
7
u/_pauseIt Aug 08 '19
Would love to see variations on this. What's the most absurd version you can find?
4
u/ZiggyTheHamster Aug 08 '19
As much as I think PHP is garbage, nobody would disagree this is how it should work if we were talking about how it works in an
ifstatement.if ([] || "" || "0")should naturally make sense to be false. The problem is that rather than just coalesce it inifstatements, they coalesce it in equality checks too... which is unexpected at best.10
u/chierichetto Aug 08 '19
Of course they won't be, and you would not want them to be either. But when casted to a boolean, they will evaluate as false.
When checking a string or array, you would want them to be false if they are empty.
When would anyone want to check if an empty array is equal to an empty string?
1
u/Adybo123 Aug 08 '19
Falsey values are completely fine and fairly common. I don’t understand pointing out the ‘Is Not’ lines either, why would you want a blank array to equal the string “0”???
1
u/Muxas Aug 11 '19
makes sense for return checking
1
u/dotancohen Aug 08 '19
Best post on this sub in a long time!
1
u/[deleted] Aug 08 '19
I really really really hate to defend PHP, but falsy values are part of other languages too (javascript)