PHP Comparison Surprises

May 5, 2011 by alex

UPDATE

@tcollen reminded me that any non-empty string evaluates to true . Ok, my mistake there. I guess my PHP is rustier than I thought…


I’d expect that if $a == $b , and $b == $c , then $a == $c . Alas, I just tripped over a case where this isn’t true.

$ php -r "echo 1 == true ? 'true' : 'false';"
  true

$ php -r "echo true == 'enabled' ? 'true' : 'false';"
  true

$ php -r "echo 1 == 'enabled' ? 'true' : 'false';"
  false

First: This is just nuts. true == 'enabled' ?!?!

Second: Where did ‘enabled’ get it’s special status? I wasn’t expecting it, and I can’t find it documented anywhere. http://php.net/manual/en/types.comparisons.php

Third: ARGH!!!

☙ ☙ ☙