Skip to content

Instantly share code, notes, and snippets.

@nathanjohnson320
Last active December 29, 2015 08:19
Show Gist options
  • Save nathanjohnson320/7642484 to your computer and use it in GitHub Desktop.
Save nathanjohnson320/7642484 to your computer and use it in GitHub Desktop.
Why PHP is weird
if ("false" == true) echo "true\n";
// => true
if ("false" == false) echo "true\n";
// => false
if ("false" == 0) echo "true\n";
// => true, wtf
if (false == 0) echo "true\n";
// => true, as expected
// so "false" = true && "false" = 0, so "false" is true and
// false is false and we haven't even discussed identity, yet
if ((string)"false" === (int)0) echo "true\n";
// => false, ...ok...
if ("0" === 0) echo "true\n";
// => false
if ("false" === false) echo "true\n";
// => false
if ((int)"0" === 0) echo "true\n";
// => true, with type coercion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment