by time_management · 18y ago · 0 comments
true == "foo" == 0 == false
I also generally dislike the idea of having certain values being treated as "false", e.g. 0 is false but 2.2e-16 is true... or "a" and "1" are true but "0" is false. It just seems to be clumsy, and not very robust. It reminds me of the "and-or trick" in Python, which busts if you aren't familiar with the fact that values such as 0/0.0 are false.
* Actually, on review, it's obvious that some non-transitivity is desirable by design, in the sense that 1 and 2 are both "true" but we don't want 1 == 2, even though 1 == true and 2== true. However, I still maintain that the chain I have above represents bad design.
I guess that, for me, the cost of a few keystrokes involved in writing "if x == 0 then ..." vs. "if x then ..." never seemed to be worth the hassle of remembering false values. I've always found it preferable to make the comparison being made explicit.
I also generally dislike the practice of using reserved values to signify empty data, e.g. 0 for missing numerical data. I prefer to use ML-style option types, e.g. None vs. Some 0, and in a dynamically typed language, I'd just use the nil/().
functions like is_numeric('string') exist for the rarer situations where things like this actually matter, but php's default way is simply reflective of it's nature as a loosely-typed language.