2

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?

2
  • I know how to use === but I just wonder, which algorithm PHP follows for things like this. Your answer seems to be wrong, because [] == "" returns false, despite of both arguments being empty(). Commented Nov 30, 2014 at 18:47
  • That's just how PHP was designed, I guess. Commented Nov 30, 2014 at 18:48

3 Answers 3

3

In JavaScript, the behavior of == is defined in The Abstract Equality Comparison Algorithm.

  • When you do "" == +0, since Type("") is String and Type(+0) is Number, the returned value is the result of the comparison ToNumber("") == +0.

    The behaviour of applying ToNumber to String type is defined in ToNumber Applied to the String Type. In our case, ToNumber("") returns +0.

    Now we must check +0 == +0. Since both have the same Type, that Type is Number, and both +0 and +0 have the same Number value, the returned value is true.

  • When you do +0 == "", since Type(+0) is Number and Type("") is String, the returned value is the result of the comparison +0 == ToNumber("").

    The behaviour of applying ToNumber to String type is defined in ToNumber Applied to the String Type. In our case, ToNumber("") returns +0.

    Now we must check +0 == +0. Since both have the same Type, that Type is Number, and both +0 and +0 have the same Number value, the returned value is true.

Sign up to request clarification or add additional context in comments.

2 Comments

++ Didn't notice the question include javascript as well. Good explanation.
It's a pity, we can't accept two answers. This one is exactly as good as the Aziz' one, but it explains JS, and his one explains PHP. +1, however.
2

In PHP when using the equal it will convert the input so that the following are all equal:

0 == "" == false

Which all will pass an empty() check.

If you want to check the exact type use === instead. To answer your comment, array() is not a variable, but rather a data structure, that is why it fails the == check but passes the empty() check.

1 Comment

That's what I wanted to know. Thanks
1

php favors integers in comparisons, so if either value is an int the comparison will be done as an int.

The "int-ness" is determined not by type, but by an is_numeric() test. Strings are silently converted to numbers even when you don't expect it, so "1.2" == "1.20" is true (!?). Some versions of php used to say two long numeric strings were equal if they had an int overflow with idential prefixes.

Javascript seems to also cast to number when comparing to a number, but two strings compare as strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.