1
echo (true == -1 ? "I'm a little bit surprising" : "Life is easy");

gives

I'm a little bit surprising

May someone explain me why is -1 equal to true? Thank you

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
CuriousPanda
  • 73
  • 1
  • 8
  • 1
    https://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp should explain how, not sure about why. – Nigel Ren Aug 14 '21 at 06:58

1 Answers1

5

Just read the manual, it says:

When converting to bool, the following values are considered false:

  • the boolean false itself
  • the integer 0 (zero)
  • the floats 0.0 and -0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • the special type NULL (including unset variables)
  • SimpleXML objects created from attributeless empty elements, i.e. elements which have neither children nor attributes.

Every other value is considered true (including any resource and NAN).

KIKO Software
  • 15,283
  • 3
  • 18
  • 33