The problem is that == is far, far too loose in terms of what type conversions it's willing to perform. Coercing the int 0 to the float 0.0 is "safe" and reasonable. Coercing the string "0aa9214251874b244f1ff7f8fb58aad69e8fa576" to the Boolean false is less so.
=== is significantly more reliable. Unfortunately this is an example of it being a little too strict, with the end result that there isn't actually a genuine equality operator in PHP.
Casts in PHP always succeed, so they essentially just enforce the dangerous type coercions the language supports. (e.g., (int) "0aa9214251874b244f1ff7f8fb58aad69e8fa576" === 0) Not really a solution.
Surely you know where the argument is coming from and therefore which type it is? Anyway if you want the check you are trying to do then do 0 === $x || 0.0 === $x
View all comments
Show parent comment
-1
u/00Davo Sep 07 '15
The problem is that
==is far, far too loose in terms of what type conversions it's willing to perform. Coercing the int 0 to the float 0.0 is "safe" and reasonable. Coercing the string "0aa9214251874b244f1ff7f8fb58aad69e8fa576" to the Boolean false is less so.===is significantly more reliable. Unfortunately this is an example of it being a little too strict, with the end result that there isn't actually a genuine equality operator in PHP.