Because of loose typing, PHP has to cast both compared variables to the same type, if they aren't yet. What happens behind the scenes for this comparison:
$v = "" == 0;
It returns true
. If the type of the first variable is internally casted to the type of the second variable, I can understand it:
$v = (int)"" === 0;
But if it were so, the inverse comparison should fail:
$w = 0 == "";
because
(string)0 = "0"
which is obviously not equal to ""
.
but it returns true
, as well.
The same behavior can be observed with JavaScript.
So now I am asking myself: what happens there?! The only explanation for me is that both variables are casted to boolean. But in this case, ["X"] == "X"
should return true, but it obviously doesn't. So, what's the magic to assume ""
equal 0
?