4

In PHP,

null==0
0=="0"

If you combine these two, you would expect:

null=="0"

But this is not true.

Could someone explain this to me?

rationalboss
  • 5,330
  • 3
  • 30
  • 50
ericj
  • 2,138
  • 27
  • 44
  • 3
    PHP is not logical. `null == (boolean) '0'` is `true` again… – feeela Sep 18 '12 at 09:23
  • 1
    The special NULL value represents a variable with no value. NULL is the only possible value of type null. A variable is considered to be null if: it has been assigned the constant NULL. it has not been set to any value yet. it has been unset(). http://php.net/manual/en/language.types.null.php – OctoD Sep 18 '12 at 09:26
  • and 0 == "0" is not correct. 0 is an integer and "0" is a string :) – OctoD Sep 18 '12 at 09:27
  • See [this answer](http://stackoverflow.com/a/80649/569101) – j0k Sep 18 '12 at 09:28
  • 1
    [PHP comparison tables](http://us.php.net/manual/en/types.comparisons.php) - just use this instead of looking for logical explanations. – DCoder Sep 18 '12 at 09:29
  • @OctoD: seems like you havn't understood what this question is about. you're right that a number isn't the same as a string, but since PHP only has weak types those comparisions can give confusing results. take a look at this: http://codepad.org/ARVz5Q7v (you see, `0 == "0"` is `true` in PHP) – oezi Sep 18 '12 at 09:30
  • maybe you should rather use `===` if you want a logical, transitive equation operator. – Andreas Grapentin Sep 18 '12 at 09:33
  • @oezi sorry for the mistake ^^ have you tried with === ? it should checks type and equality... – OctoD Sep 18 '12 at 09:35

3 Answers3

10

In the first case:

null==0

null evaluates to false , same as 0 which evaluates to false, so both are false and so the comparison returns true.

In second case:

0=="0"

here you are comparing two variables of different type, one is numerical and other string, because you are not using the === operator, PHP cast one of them to the other type so 0 casted to string equals "0" which so they are the same, if it's "0" which is casted to number also casts to 0 so its the same as the other value, and so this comparison returns true.

In third case:

null=="0"

here's the same situation, both are different types so PHP cast one of them to the type of the other, but if you cast null to string the result is "null" which is not equal to "0", so that's the reason is not true that comparison.

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • the third case explanation cannot be true, because null=="" (and "null" != ""). Thanks anyway. – ericj Oct 15 '12 at 09:13
2

== checks equality

=== checks equality AND type (we also say it is "identical")

Therefore, since PHP does not have strong type hinting, it is automatically casted to the best suited type.

null === 0 is false while null == 0 is true because 0 or '0' are considered as null value as well as false. An empty value null == '' will return true too.

That's how PHP works.

A best practice is to always check for type using the === operator (and its negative equivalent, !== and use only the other in special cases).

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
1

You have to understand that because PHP is not strict in its typing it is often casting your variables to other types depending on the comparison or operation needed. In the case of null==0 it is telling you that both null and integer 0 are considered false.

In the case of null == "0" it is checking if the string "0" is empty which it is not. Comparing integer 0 with string "0" type cases "0" to an int for comparison in which case they are equal.

Hope that helps.

davidethell
  • 11,708
  • 6
  • 43
  • 63