Comparison Operator - Type Juggling and Boolean

I read PHP docs like Juggling and Booleans, but I still don't understand why this comparison is being evaluated as true. My [wrong] understanding tells me that in the if statement below, the integer 0 is considered FALSE and "a", being a non-empty string, is considered TRUE. Therefore, I expected this comparison to resolve to FALSE == TRUE and, ultimately, FALSE. In what part have I been mistaken?

   <?php
            if(0 == "a"){
                    $result = "TRUE";
            }else{
                    $result = "FALSE";
            }

            //$result == "TRUE"
    ?>

http://codepad.viper-7.com/EjxBF5

+5
source share
3 answers

PHP string <= > integer, . , "42", 42 . , .

:

. , . 0 ().

( ).

+9

, , operator == , . .

, , ( "a" converts to 0), 0 == 0.

+4

It will work if you use strict comparison ===instead ==. A strict comparison also checks the type of variables, so it 0 === 'a'will be false.

+2
source

All Articles