0

I've got some weird actions from my php script and finally figured out that this following statement makes problems. Because 0 is equal some string in php.

if(0 == "whats Up?")
{
  echo 42;
}

With triple "=" it do what I expected. It is possible for you to give me a briefly answer what is the reason and idea behind this behavior of php? Why did they implement php like this?

I mean I know that 1 == "1" is true and 1 === "1" is not. This is also in python. I also learn from somewhere that 0 could be understandable as false but this example above has no explication for me. But I am sure that you know it.

Thank you in advance

Ilja Everilä
  • 45,748
  • 6
  • 100
  • 105
Allan Karlson
  • 433
  • 9
  • 23

1 Answers1

1

That's because of Type Juggling. The second operand gets converted to an integer and 0 == 0 is true.

var_dump((int) "whats Up?"); // int(0)
ShiraNai7
  • 6,129
  • 2
  • 23
  • 26