Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly is null defined in PHP?

Tags:

php

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
?>
like image 590
Prabhas Gupte Avatar asked Jul 25 '26 14:07

Prabhas Gupte


1 Answers

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

enter image description here

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

like image 57
Dinkar Thakur Avatar answered Jul 27 '26 02:07

Dinkar Thakur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!