I’m new to PHP and I just ran across a day-wasting bug because I didn’t realize that the PHP == operator does type coercion similar to Javascript.
I know that Douglas Crockford recommends never using the == operator with Javascript, and to always use the === operator.
If I code in a manner that never assumes type coercion, can I use the same advice in PHP, and never use the == operator? is it safe to always use the === operator, or are there gotchas that I need to be aware of?
You should use
===by default (to avoid the problems you just encountered) and use==when needed, as a convenience.For instance, you may be taking parameters from
$_GETor similar, and a parameter might be the stringtrueorfalse, vs the booleantrueorfalse. Personally, I check everything, but there can be legitimate use cases for==if you are conscious of it, and careful with use.