December 10th, 2009
Logic bug in php 5.3
So… here we go again. After the ugly php 5.2 output buffer crashes, we now have the php 5.3 logic bugs:
$foo = 0;
var_dump('foo' == $foo); // true
var_dump($foo == 'foo'); // true
var_dump('foo' === $foo); // false
var_dump($foo === 'foo'); // false
If your scripts are returning completely unexplainable results on php 5.3 platforms, that's one possibility to keep in mind. I initially found it while looking into memcached-related problems.
The worst part of it, though, is that it's not a bug: it's a feature.
Filed under Blog, Highlights, WordPress by on Dec 10th, 2009.
Comments on Logic bug in php 5.3
You're shure that this is new to PHP 5.3?
You're totally right, yeah. Further investigation revealed it wasn't new at all.
What had confused me into thinking it was, is that I had never run into the issue before, and there is an implicit string to number conversion when comparing numerical strings:
var_dump(0 == '0'); // true
var_dump(1 == '1'); // true
var_dump(1 == '2'); // false
You learn stuff everyday, as they say. Even when you're a php junkie. :-D
Adding to the previous comment, and further adding to the confusion, php has this obnoxious feature:
var_dump(" == '0'); // true, because both are empty
var_dump(" === '0'); // false
This is why you have to kick and scream in situations like Ticket #7748. Data types are twice as important in languages that don't enforce them.
If only it were the only such ticket, lol. :-D