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?

1

2 Answers 2

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.

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

2 Comments

@MarcelKorpel, absolutely. I have a strict rule to never use == at all.
I'm just stuck by a higher PHP version. Always use === to compare those strings.
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

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.