Code:
$x = true;
if ($x == "continue") {
$x = "foo";
}
echo $x; // prints "foo"
I know I can use ===
instead to solve it, but I wonder why PHP thinks that a string is equal to true?
Code:
$x = true;
if ($x == "continue") {
$x = "foo";
}
echo $x; // prints "foo"
I know I can use ===
instead to solve it, but I wonder why PHP thinks that a string is equal to true?
Because a non-empty string is considered true
and an empty string is false
.
Google for "truthiness" for more info. And yes, a lot of it is bonkers. Just wait until you hear about how JavaScript does it.