8 ms·
PHP's == has a lot of oddball effects. They were put in so that things would behave the way a novice expects them to (3 == '3') but would confuse more experienc
by Meekro 11y ago
PHP's == has a lot of oddball effects. They were put in so that things would behave the way a novice expects them to (3 == '3') but would confuse more experienced programmers, or those coming from other languages.
Unless you're deliberately taking advantage of automatic type conversion and whatnot, you should probably use === by default.
- EugeneOZ 11y agoDesigners of future languages, please take this example as a proof of the rule: don't design anything for newbies. They will find a way to make an error anyway, but dumbs-based design will be the problem for everyone else.
- troels 11y agoThat - and the error is going to be a lot more subtle and harder to find. In all fairness though, it's a balancing act - There are benefits to dynamic typing, but PHP clearly overdid it. (See also the disaster that was/is magic quotes)
- bencoder 11y ago> you should probably use === by default. unfortunately this can also backfire if your class/module is used in a different context where it gets strings instead of integers and you were just using === without really thinking about it: We had a case where the code was something like: function doSomething($value) { if ($value === 0) { //do something } else { //do something else } } This was then used in a slighly different context where $value was a string '0', it then ended up incorrectly in the //do something else block, doing the completely wrong thing. In this case the type co-erced == would have been better, and I think what the developer was expecting would be a type error due to the === but it's not a type error, it'll just fall into the else block.
- deleted 11y ago[deleted]
- WalterGR 11y agoYou're describing the expected behavior of === and a bug. This is not the === operator "backfiring."
- bencoder 11y agoAbsolutely, I was just suggesting that "you should always use the === operator" advice which I see a lot of people say(examples multiple times in this thread), does not guarantee you won't run into problems with incorrect types, and giving an explanation. As always, you should be thinking when programming.
- TeMPOraL 11y ago> They were put in so that things would behave the way a novice expects them to (3 == '3') It's a very wrong approach. It may look like newbie-friendly, but in fact it makes it much harder to learn and use. Any novice will be constantly attempting to form a mental model of what's going on and how the language interprets concepts. Refusing to do things like 3 == '3' is simple and makes sense. Assuming a programmer is an idiot and trying to outguess his mistakes makes the language so complicated, that the novice will not be able to form a coherent model and will most likely assume that "this thing is magic".
- Schlaefer 11y agoIt's hard for newbies who want to master the language. It's not hard for people who have no interest in learning a programming language and just wan't to make the thingy in their HTML do some stuff. Register globals, <?php if ($category == 2) { echo 'Foo'; } ?> and be done. We have to remember the PHP origins and audience from way back to understand why this was considered easy to use.
- Killswitch 11y agoshhhhh, people don't realize PHP started out as just a tool for Rasmus and ended up evolving. No, to them, PHP was DESIGNED this way on purpose from the ground up.
- eropple 11y agoDo you consider that an acceptable excuse for its behaviors fifteen years after the fact? Because I do not.
- Killswitch 11y agoOf course not, but everyone keeps comparing PHP to languages that were designed and developed to be languages, not a toolset that some crappy developer (his own words) created for his personal site that ended up evolving and becoming a real language. It's got quirks, we get it. Let's keep improving the language as we go instead of constantly bashing it. I mean PHP is one of the most widely used languages on the web today.. Clearly it's doing something right.
- sarciszewski 11y agoAnd in C we can do this to get TRUE: return (33 == '3'); :P
- nathanb 11y agoIncorrect. However, (0x33 == '3') will return true, as will (51 == '3'). Your point is valid, even if your code is wrong. Automatic type coercion can produce unexpected results in any language. PHP's automatic type coercion rules are designed to help newbies at the expense of experienced developers. C's automatic type coercion rules are, largely, designed to expose the underlying memory layout to developers who know what they're doing, at the expense of inexperienced developers. Both can easily contain dangerous pitfalls, but I prefer the latter philosophy over the former. (Disclaimer: I have built a career as a C programmer and frequently use its lower-level features to great advantage. I am biased.)
- sarciszewski 11y agoOkay, I stand completely corrected.
- dfox 11y agoAnd there are no type-coercion rules at play in case of 51 == '3', because type of '3' is int (as per ISO 9899 p. 6.4.4.3.2).
- nathanb 11y agoExcellent point. Thank you for the clarification. This is true in c99 as well.