4 ms·
> After all, most programmers don't expect: <?php 0 == "string"; ?> to be true. nobody would expect that
by merb 10y ago
> After all, most programmers don't expect: <?php 0 == "string"; ?> to be true.
nobody would expect that
- dozzie 10y agoExperienced Perl programmers would, and PHP took inspiration (altough in crippled way) from Perl.
- oneeyedpigeon 10y agoAnyone 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.
- xerophyte12932 10y agoWait, is GP saying that IS true in php?!?!
- s_q_b 10y agoYeah, go check in a sandbox.
- DCoder 10y agoIt gets worse: https://3v4l.org/Slvpp 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.
- Terr_ 10y agoMy 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"