Understanding PHP type coercion
I think this article explains it pretty well:
Type-coercing comparison operators will convert numeric strings to numbers
Just to quote the main issue here:
According to php language.operators.comparison, the type-coercing comparison operators will coerce both operands to floats if they both look like numbers, even if they are both already strings:
where both strings are using exponential notation, hence are treated as numeric strings, making loose comparison (==
), coerce these strings to floats before actually "loosely" comparing them.
As a best practice and to prevent unexpected behaviour, always try to use identity equality (===
), especially when dealing with strings.