5

I got lost.. if someone could explain to me why the following query:

var_dump("0000F607"=="00000000"); is returning: bool(false)

but fallowing query: var_dump("0000E607"=="00000000"); return bool(true)

I know that == only compares variables (without checking type of variable), but here I compare 2 strings..

ADyson
  • 57,178
  • 14
  • 51
  • 63
designer132
  • 131
  • 7
  • 1
    @Machavity: You forgot to mention that `"0000E607"` gets converted to `0.0E607`. I was writing an answer, but you closed the question. It would be nice to know why this comparison return false. – KIKO Software May 05 '22 at 12:17
  • https://stackoverflow.com/questions/58575524/php-loose-comparisons might be more useful - the key is that any string in the format `XeY` where X and Y are numeric will be evaulated as scientific notation – iainn May 05 '22 at 12:18
  • 2
    PHP type juggling is complicated for historical reasons, but I fail to see what exact weird rule can be kicking in here. – Álvaro González May 05 '22 at 12:18
  • The [equal operator](https://www.php.net/manual/en/language.operators.comparison.php) `==` doesn't only compare variables, it compares expressions. Expressions *can* be a variable *or* a constant value *or* the returned value of a function etc.. – Peter Krebs May 05 '22 at 12:19
  • @ÁlvaroGonzález One number converts to a zero integer, the other to a `0.0E607` float. To compare them both become floats, so `0.0 == 0.0e607` and these are not equal due to [the limits in the way floats are represented](https://stackoverflow.com/questions/588004/is-floating-point-math-broken/588014#588014). – KIKO Software May 05 '22 at 12:22
  • "hello" and "bye" will also juggle to 0 when explicitly casting to (int). I tried `var_dump("00000f607"=="000f607")` returns bool(false) – tnavidi May 05 '22 at 12:30
  • 2
    PHP will only juggle strings to numeric types when they are *both* in numeric formats. `0E0` is scientific notation, so the comparison is performed numerically. `0F0` is not a numeric format, so the strings are compared as strings. There's no mechanism for type juggling when only one of the strings looks like a number - it has to be both of them (or if a string is being compared to an *actual* numeric type). Your second example qualifies, your first does not. – iainn May 05 '22 at 12:37
  • 2
    @KIKOSoftware Asked in the PHP room and they suggested [this answer](https://stackoverflow.com/a/12598484/2370483), which is dealing with the same exact type of problem. The `E` is making PHP type juggle one as a number, while the other one has no characters that would trigger that rule. One of those arcane PHP rules. – Machavity May 05 '22 at 12:57
  • @Machavity Interesting... The documentation quote at [Why does PHP convert a string with the letter E into a number?](https://stackoverflow.com/questions/12598407/why-does-php-type-juggle-a-string-with-the-letter-e/12598484#12598407) explains it all ("If [...] comparison involves numerical strings, then each string is converted to a number") but it was seemingly removed from the manual. – Álvaro González May 05 '22 at 13:11
  • @ÁlvaroGonzález That's a good point. I'll see if I can get the manual updated. I renamed that question and cleaned it up so it's easier to find in the future – Machavity May 05 '22 at 13:12
  • 1
    It's still in the manual (on the same page), it's just been reworded to *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.* – iainn May 05 '22 at 13:30
  • 1
    I found where the manual formally notes this is seen as a number: https://www.php.net/manual/en/language.types.numeric-strings.php (see `EXPONENT_DNUM`) – Machavity May 05 '22 at 13:41
  • I got the manual updated (see previous link) so there is something more explicit about scientific notation – Machavity Jul 08 '22 at 22:54

0 Answers0