4 ms·
Why are you using "=="? Use "===" and make explicit casts as required.
by sadfklsjlkjwt 6y ago
Why are you using "=="? Use "===" and make explicit casts as required.
- out_of_protocol 6y agoSometimes "==" is used under the hood ( switch/case i think?) and it's hard to avoid it completely even if you always use "===" in the code
- mywittyname 6y agoYou don't need to use switches. In fact, I'd argue that if blocks are better if you're evaluating against variable data types. Alternatively, if you really, really want to use a switch statement and loose typing might cause issues, it's pretty easy to compare types before the switch statement, e.g, if(gettype($a) !== "...") { return false; } switch($a) { ... }
- out_of_protocol 6y agoEither choice doesn't really look like the sane language anymore
- mywittyname 6y agoWhen you have dynamically typed languages, whether to coerce or not is a design decision with usability trade-offs. Javascript exhibits pretty similar behavior with switch statements for exactly the same reason: it also has two types of equality comparisons. Also, PHP allows you to add explicit type casting to case statements. e.g., case (string) 1:
- aganame 6y agoAnd what is the equivalent of "===" for ">" and "<"?