The Boolean paradox

A small comparison just cost me half an hour of valueable time.

In an ongoing series, I shall present another PHP WTF: The Boolean paradox.

// ...

public function setStamp($tm)
    {
        if ('NOW' == $tm)
            $this->stamp = date('Y/m/d');
        else if (is_int($tm))
            $this->stamp = (0 == $tm ? '0000-00-00' : date('Y/m/d', $tm));
    }

//...

$foo->setStamp(0);
echo $foo->stamp;

// Output: 2012/08/10

As you can see, the example above should output “0000-00-00” but gives us the current date. Let us disect, shall we?

$a = (int) 0;
$b = 'NOW';

// Let the tests commence...
if ($a == $b)
     print "a equals b\n";

if ($a)
     print "a is TRUE\n";
else
     print "a is FALSE\n";

if ($b)
     print "b is TRUE\n";
else
     print "b is FALSE\n";

Output (see this codepad):

a equals b
a is FALSE
b is TRUE

We shall conclude FALSE == TRUE in PHP. Q.E.D.