Unless you're deliberately taking advantage of automatic type conversion and whatnot, you should probably use === by default.
Unless you're deliberately taking advantage of automatic type conversion and whatnot, you should probably use === by default.
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".
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.
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.
McDonald's is super popular, too, and deserves even more of a ration of shit than they get for feeding people slop.
Not that this is particularly important, I guess.
¹no DB, but the "just write your visitor counter into a plain text file" back then
News to me. You have to enter a really high-precision number as a string in Java so it won't be rounded off to fit within a double. This is an unsolved problem.
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)
return (33 == '3');
:PPHP'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.)
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.This is not the === operator "backfiring."
As always, you should be thinking when programming.