3

I learned that the empty string "", 0 and "0" all mean false in php. I wonder does php take that into account when it comes to comparing equality.

    $str = "";        
    echo ($str ==  "0") ? "yes" : "no"; // prints "no" (A)

    echo ($str ==   0)  ? "yes" : "no"; // prints "yes" (B)

Line A suggests that php is comparing $str and "0" as if they are all strings, ignoring that they both can mean false. But line B is comparing their "false" interpretation.

So is it the case that php firstly checks if the two values have the same type, if so it checks equality assuming the same type; if not, it uses the boolean meanings of the values (is there type casting involved?)

4
  • In the first case you are comparing two strings. So naturally "" is different then "0". On the second one there is more typecasting happening because of the different data types. Use triple equal === and you don't have to worry about the ambiguity of typecasting Commented Feb 26, 2015 at 10:05
  • 2
    This might be of help: php.net/manual/en/types.comparisons.php Commented Feb 26, 2015 at 10:06
  • Only when you convert to boolean the "0" string is interpreted as false, not in other cases. Commented Feb 26, 2015 at 10:07
  • If you are interested in many more of those interesting PHP behaviors, I suggest the all-time classic here. Commented Feb 26, 2015 at 10:08

3 Answers 3

4

I learned that the empty string "", 0 and "0" all mean false in php.

This statement is false. Empty string, 0 and "0" are false when casted to boolean type. Otherwise they are either empty string, integer zero or string with one character, respectively.

== checks values of two variables. If their types are different, some casting happens and unpredictable (for rookies) results come up.

=== checks values of two variables and their types.

Anyway, when you compare "0" == "", PHP interpreter compares two strings which are different. But when you go with 0 == "" it first changes numeric string to integer. Empty string equals 0. So we end up with 0 == 0 which is true.

Note: "8abc" becomes 8 after casting to integer. abc8 becomes 0 when casted

Manual on:
- Types casting (juggling)
- Types comparison

Sign up to request clarification or add additional context in comments.

Comments

0

There are two equality comparator in PHP

When the types are the same, they behave in the same way.

When the types are different, it all depends: PHP does not cast both values to booleans. It depends on the types of both operands, and there is a table to know what PHP will do (see second link).

I recommend reading this stackoverflow question How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

Also, the PHP manual for comparison http://au.php.net/manual/en/language.operators.comparison.php

// double equal will cast the values as needed followin quite complex rules
0 == '0' // true, because PHP casted both sides to numbers

// triple equals returns true only when type and value match
0 === '0' // false

Comments

-1
$str = "";        

//Comparing two strings, no implicit type conversion happens. Since
// "" is not equal "0", result is FALSE
echo ($str ==  "0") ? "yes" : "no"; // prints "no" (A)

//Comparing a STRING to an INT,implicit conversion happens to convert
// the string "" to an INT. Since "" has no digits, it's evaluated
// to 0. Hence, result of comparison is TRUE    
echo ($str ==   0)  ? "yes" : "no"; // prints "yes" (B)

Use "===" for more accurate comparison.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.