2

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?)

Cœur
  • 32,421
  • 21
  • 173
  • 232
Ziqi
  • 1,959
  • 3
  • 28
  • 53
  • 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 – Ibu Feb 26 '15 at 10:05
  • 2
    This might be of help: http://php.net/manual/en/types.comparisons.php – Elias Feb 26 '15 at 10:06
  • Only when you convert to boolean the "0" string is interpreted as false, not in other cases. – DonCallisto Feb 26 '15 at 10:07
  • If you are interested in many more of those interesting PHP behaviors, I suggest the [all-time classic here](http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/). – Boldewyn Feb 26 '15 at 10:08

3 Answers3

3

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

Forien
  • 2,644
  • 2
  • 10
  • 26
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
Community
  • 1
  • 1
Eloims
  • 4,807
  • 4
  • 22
  • 39
-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.

MegaAppBear
  • 1,182
  • 1
  • 8
  • 10