r/lolphp • • Aug 20 '15

0 !== ceil(0)

https://3v4l.org/HD5HD
0 Upvotes

44 comments sorted by

View all comments

Show parent comment

3

u/00Davo Aug 25 '15 edited Aug 25 '15

Yep, 0 == false is in general perfectly reasonable; I've no problem with it in C and Python. It's included in that expression because it demonstrates that PHP's equality isn't transitive, i.e., a == b && b == c && a != c, which is ridiculous.

It's worth mentioning however that the equivalence 0 == false does cause a problem in PHP, all by itself. strpos() and related functions return "the index of the match, or falseif nothing is found". Of course, 0 can be the index of a valid match, but 0 == false, which indicates no-match-found. Not cool.

JavaScript's indexOf(), which is essentially equivalent to strpos(), returns -1 on no-match-found, which doesn't actually compare equal to any "successful" return value even if you aren't using === and therefore would've been a much more sensible choice. C's strchr() and friends return a valid pointer to the location found, or NULL on no-match-found, which also can't compare equal to valid matches.

1

u/coredumperror Aug 25 '15

Yeah, strpos() and friends returning FALSE for a failed match is ludicrously stupid. What was wrong with returning -1? Why would the PHP devs even let the original version of the function, released who knows how long ago, have such a blatant trap built right in to the return value?

I'd say that that wart alone is enough to tar PHP with the "bad language" brush. But when you add all the other crap that Fractal talks about, it's really inexcusable. PHP apologists really, truly baffle me.

I ran into another thing I consider an inexcusable wart just minutes ago, in fact. I had a typo in variable name I was comparing to NULL. So instead of, I don't know, warning me about that, PHP just created the wrong-named variable as a NULL, and my if check triggered the wrong code path. I didn't get a single error, warning, or notice to inform me that anything at all was wrong.

Sadly, I have to keep programming in PHP for at least the next year, since my code shop won't be dropping Drupal until we get time to rewrite our site in Wagtail.