Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are 0=='' and '0'=='' same?

Tags:

php

equals

Consider the following PHP statements:

echo ( 0  == '' ? 1 : 0); // Output is 1
echo ('0' == '' ? 1 : 0); // Output is 0

The first will print 1, and the second will print 0. Shouldn't they both print 0? Is this expected behavior? I'm guessing its because the second is a string and the first is not, but I'd like another answer.

like image 832
theEdgeOfChaos Avatar asked Feb 16 '26 13:02

theEdgeOfChaos


2 Answers

Read Loose comparisons with ==

http://php.net/manual/en/types.comparisons.php

like image 73
diEcho Avatar answered Feb 18 '26 01:02

diEcho


In Php empty string, NULL and 0 are equal. In second case '0' is another string, and '' is another. So you get result false.

"", 0, "0", False, array(), Null are all considered False in PHP.

You can use === to make them different.

like image 41
Chuck Norris Avatar answered Feb 18 '26 02:02

Chuck Norris



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!