0

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?

Black
  • 18,150
  • 39
  • 158
  • 271

1 Answers1

2

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.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Thank you. I bet there are tens of thousands of unidentified bugs out there because of this ...*facepalm*. – Black Dec 27 '22 at 10:13