PHP int comparison - why add 0 to variables first?

Simple question. It's just interesting if there is something that you can learn from this piece of code or not.

This is found in some outdated code ... I have never seen it before, can not find anything about it on the Internet, and the developer has long been gone.

if ($row['cnt']+0 !== $count+0) {
   // log warnings etc
}

My hunch ... no, actually I don’t know why zeros are added to variables before comparison. Any ideas?

Thank you for sharing your thoughts.

+3
source share
1 answer

this is just an old way to do an int conversion:

php > var_dump('112'+0);
int(112)

by the way then:

php > echo ("0012"+0 === "12"+0 )? "yay!" : "booh!";
yay!

its clean garbage, but it works, the correct way is as follows:

php > var_dump((int)"324");
int(324)
+1
source

All Articles