4

Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?

Why this

var_dump(0 == "string");

outputs this

bool(true)

Isn't the context of == operator supposed to convert 0 into FALSE and "string" into TRUE according to this set of rules?

Community
  • 1
  • 1
Desmond Hume
  • 6,732
  • 13
  • 57
  • 104
  • 2
    try `===` and it will give you false i think. – Pankit Kapadia Dec 20 '12 at 10:54
  • But even as a comparison, that should be false, no? – Sterling Archer Dec 20 '12 at 10:55
  • 1
    To explain that behavior: You're comparing an integer and a string. PHP converts the string to an int, which is 0 (as it doesn't contain any number representation). 0 == 0 is obviously true. – Daniel M Dec 20 '12 at 10:55
  • 1
    First rule of PHP. Learn not to be surprised by anything. – Joe Dec 20 '12 at 10:56
  • 1
    This is the set of rules you need to refer to: http://php.net/manual/en/language.operators.comparison.php – Niko Dec 20 '12 at 10:58
  • @Joe - second rule of PHP - If PHP exhibits Documented behaviour then it shouldn't be a surprise – Mark Baker Dec 20 '12 at 10:59
  • @MarkBaker that arguably knocks mine off top spot. But I do find the type juggling thing a bit odd. I'd rather have a type cast exception than undocumented behaviour. No need for a religious war, I just found this question amusing. – Joe Dec 20 '12 at 12:14

6 Answers6

13
var_dump(0 == "string");

is doing a numeric (integer) comparison

0 is an integer, so "string" is converted to an integer to do the comparison, and equates to an integer value of 0, so 0 == 0 is true

Se the comparison with various types table in the PHP documentation for details

Mark Baker
  • 199,760
  • 28
  • 325
  • 373
  • 2
    It's still strange that in PHP a non-empty string is equivalent to integer `0`, which goes in conceptual conflict with C, by which PHP is claimed to be influenced. In C, a non-null __something__ casts to `true` or the equivalent integer `1`. – Desmond Hume Dec 20 '12 at 11:13
  • @Desmond If you cast `"string"` to a number and the string does not contain any numeric value, `0` is the only logical choice. If you cast `"string"` to a `bool`, it'll cast to `true` unless it's empty (for PHP's definition of "empty"). Casting `"string"` to a number and getting `1` is just as illogical and anything else, you just need to learn the rules. – deceze Dec 20 '12 at 11:23
  • Hi could you please explain to me why **filter_var(0, FILTER_VALIDATE_INT)** return false?. – HoangHieu Aug 24 '18 at 16:25
2

The table shown here is more fit for your case.

It shows TRUE for comparing 0 with "php".

Within the comparison you do not convert both operands to a boolean, but one operand will be converted to match the type of the other operand. In your case the string gets converted to an integer, which results in another 0. This gives you 0 == 0, which yields true.

Sirko
  • 65,767
  • 19
  • 135
  • 167
1

They are not of the same type, use === if you want to check if they are also of the same type.

Kevin
  • 2,639
  • 29
  • 56
1

PHP: ==

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

"string" is not number format, so it will be convert to 0.

xdazz
  • 149,740
  • 33
  • 229
  • 258
1

during the comparison, the string is converted to an integer:

var_dump(0);
var_dump((int)"string");
var_dump(0 == "string");

last line will be automatically converted to:

var_dump(0 == (int)"string");

so this return will return:

int(0)
int(0)
bool(true)
bool(true)
freedev
  • 17,230
  • 4
  • 83
  • 98
-1

You're looking for the comparison table on this site first: http://php.net/manual/en/language.operators.comparison.php. Casting to bool doesn't apply here.

Operand 1           Operand 2
...
string, resource    string, resource    Translate strings and resources to numbers,
or number           or number           usual math

"string" cast to a number equals 0.

deceze
  • 471,072
  • 76
  • 664
  • 811