1

When I run the follow code in PHP

if('yes' == 0)
    echo 'a';
else
    echo 'b';

The output is a.
I don't understand what happen? And can I convert the php code to C source code to have a look what real happening?

Lock
  • 35
  • 1
  • 4

1 Answers1

1

PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.

Eternal1
  • 5,447
  • 3
  • 30
  • 45