Hacker News new | ask | show | jobs
by merb 3487 days ago
> After all, most programmers don't expect: <?php 0 == "string"; ?> to be true.

nobody would expect that

2 comments

Experienced Perl programmers would, and PHP took inspiration (altough in crippled way) from Perl.
Anyone who ever programs in a weakly-typed language should be aware that comparing two values with different types will involve type coercion, that there are obviously strange edge cases when that happens, and that comparing with a strict comparison operator such as === is good defensive programming in those contexts.
Wait, is GP saying that IS true in php?!?!
My favorite is that two obviously different hexadecimal values in strings (e.g. checking password hashes) can be "equal" with the weaker == comparison.

This occurs if PHP thinks both strings could be numbers in scientific notation. "0e123" == "00e45"

It gets worse: https://3v4l.org/Slvpp

Note that PHP also has a JavaScript-style triple-equals comparison, which does not attempt type conversion and does not exhibit this bizarre behaviour.

Yeah, go check in a sandbox.