Search code examples
phplaravellaravel-8laravel-7laravel-6

String comparation strange behavior in php


Today I noticed very strange behavior when I compare 2 different strings

dd('115e-401' == '115e-402');

this is return true. But why?;

enter image description here


Solution

  • Type juggling kicks in when the strings appear to be numbers.

    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.

    E-401 is a very small number which is smaller than what PHP can represent. The smallest number is PHP_FLOAT_MIN about 2.2E-308 (system dependent).

    Therefore, your strings are converted to numbers. They are smaller than what PHP can represent and are converted to zero. 0 == 0 is true.