1
var_dump("555555555555555555555" == "555555555555555555553"); //bool(true)
var_dump("aaaaaaaaaaaaaaaaaaaaa" == "aaaaaaaaaaaaaaaaaaaab"); //bool(false)

Why does this happen?

I know I can use

var_dump(strcmp("555555555555555555555", "555555555555555555553") == 0); //bool(false)

But why the first row returns true?

YakovL
  • 7,557
  • 12
  • 62
  • 102
Svet
  • 13
  • 2
  • possible duplicate of [What is the difference between == and === in php](http://stackoverflow.com/questions/6316784/what-is-the-difference-between-and-in-php) – mario Oct 03 '13 at 14:24

2 Answers2

4

It's a side effect of type-coercing. There's an article on phpsadness about it. Basically, the strings in the comparison are converted to numeric types, and due to precision loss, appear to be equal.

Denis
  • 5,061
  • 1
  • 20
  • 22
1

In your first row

var_dump("555555555555555555555" == "555555555555555555553");

it is true

Why because, the type-coercing comparison operators will coerce both operands to floats if they both look like numbers, even if they are both already strings

This bug is discussed here

Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57