It’s Not a Bug :-p August 3, 2007
Posted by ordinarywebguy in PHP, Programming.4 comments
Just a follow up that I indeed deferred to my post: in_array PHP Function Bug.
I posted the said blog post in PHPUGPH and lead me to fully understand that it was not a bug. As Cruizer said:
if it’s not a bug, then it’s a gotcha. i don’t think that would be obvious to any programmer.
bad design, i’d say.
I definitely agree with that statement.
To clear up everything that it was not a bug. See code:
$str = “this is a string”;
if ($str == 0) {
print “true”;
} else {
print “false”;
} # end if
Output: true
Wonder why the output is “true”?
See here: http://www.php.net/manual/en/types.comparisons.php
In addition, RJ said:
As I understand the checking by PHP for the loose comparison of a string and an integer, the PHP engine will call the intval() function for the string before comparing it with the integer. Therefore “any string” == 0 will actually be intval(“any string”) == 0 which will result to 0 == 0. It means that it is not a boolean comparison.
Again, that’s not a bug, gotcha, or whatever you call it but instead a bad design.
bad design, i’d say.