How come null
which is a predefined value in PHP equal to 0
and less than -1
at the same time?
Refer to code below:
<?php
var_dump(null == 0); // evaluates to true
var_dump(null < -1); // this also evaluates to true
?>
How come null
which is a predefined value in PHP equal to 0
and less than -1
at the same time?
Refer to code below:
<?php
var_dump(null == 0); // evaluates to true
var_dump(null < -1); // this also evaluates to true
?>
it not the value of null which is giving you the results its the dynamic conversion happing during comparision
For the php manual
For various types, comparison is done according to the following table (in order).
http://php.net/manual/en/language.operators.comparison.php
You can see that if Operand 1 is bool or null and Operand 2 is anything than convert the side to bool applies, and also 0 is false in php, also in PHP treats null ,false, 0, and the empty string as equal. so
var_dump(null == 0);
is like var_dump(false == false);
which evaluates to true
and var_dump(null < -1)
is like var_dump(false < true)
which is again true
hence you are getting these results