Chethan B
Chethan B

Reputation: 63

PHP type juggling clarification

Can someone explain why this is returning true?

I tried removing any one character and it returns false but exactly this code returns true I cannot understand why

var_dump('608E-4234' == '272E-3063');

Upvotes: 2

Views: 69

Answers (1)

Alexander Yancharuk
Alexander Yancharuk

Reputation: 14491

PHP documentation says:

If both operands are numeric strings, or one operand is a number and the other one is a numeric string, then the comparison is done numerically

In this case you're trying to compare 2 floats:

var_dump((float) '608E-4234'); // double(0)
var_dump((float) '272E-3063'); // double(0)

The reason why those floats are equal is that their values out of precision settings. Try to compare 272E-306:

var_dump((float) '272E-306'); // double(2.72E-304)

and you'll see another result.

Upvotes: 3

Related Questions