jump to navigation

It’s Not a Bug :-p August 3, 2007

Posted by ordinarywebguy in PHP, Programming.
trackback

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. Tongue bad design, i’d say.

I definitely agree with that statement. :D

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. :(

Comments»

1. remmelt - August 18, 2007

Ah! I just replied to your other post! Haha, that’ll teach me…

2. eidderf - September 4, 2007

Hi I saw your PHP google class and I thought you might want to do a freelance job for us. We are a start up web 2.0 company here in Kuwait and we are looking for a kick ass PHP or Ruby and Ajax developer. I think you might be one of them, care to know? We give more pay to the most kick ass guy. Please email me

3. Website Scripts » It’s Not a Bug :-p Just An Ordinary Web Guy - January 27, 2008

[...] Dig deeper into the topic here [...]

4. Aman - February 1, 2008

I think if we could keep in mind about the php type juggling, (“hello”==0 ) will make sense :)
This is really a bad idea/design, though it also makes many works easier in other contexts.