8 ms·
Why MD5('240610708') is equal to MD5('QNKCDZO')?
- kafrofrite 4y agoUh the joys of PHP's type juggling. Fairly sure this bug is still present in many systems.
- dvh 4y agoLinter would cought this error. Seriously use linter, define a shortcut for it. It catches stupid errors like these, missing brackets, uninitialized variables, etc...
- sebzim4500 4y agoThis makes me so glad I don't use PHP.
- thunderbong 4y agoEvery language has it's quirks.
- Izkata 4y agoAnd this one had a purpose. Remember, PHP was designed (as much as it was "designed") for less-technical people. And everything comes through $_GET and $_POST as strings. The way I see it, this was a crude attempt to make things like $_GET['age'] > 18 work without having to remember to convert types manually. Javascript was the same with input.value The == conversions become more intuitive if you keep this use case in mind and think about semantic equality instead of programming's more traditional exact equality.
- giantrobot 4y agoThat's not a quirk. That's a fundamentally broken design. It flagrantly violates the principal of least surprise. The language is almost helping people write bugs in their code.
- dale_glass 4y agoIt's the kind of thing you get with weak typing. Perl does better here by requiring "eq" for strings, so there's less chance for such an issue to happen. But you have the "zero but true" fun quirk -- "0e0" evaluates to 0 numerically, but 1 logically because it's not an empty string.
- eknkc 4y agoYeah but there are levels of weak typing. JS does not do this as both operands are strings, it compares them as strings. That makes tons of more sense compared to this. Not that JS is the most sane language out there but PHP tends to make it a lot easier to shoot yourself.
- tuyiown 4y agoThe casting occurs for both operands that have same type, in narrower type space, with non trivial rules, behind your back, is really acid level of a surprise. The kind that would make the removal of the "==" operator reasonable.
- VHRanger 4y agoPython does weak typing and doesn't have 1% of the unexpected nonsense PHP has.
- mkl 4y agoYou're probably thinking of dynamic typing, which Python has, but Python's typing is strong, not weak, so e.g. '1' == 1 returns False.
- zarzavat 4y agoPython has strong typing, but it does use duck typing. Weak typing is when a language inserts implicit coercions in unexpected places, such as 42 == “42”. It’s not a precisely defined term. Another example is in C where pointers and values may be mixed and matched, although less so nowadays.
- giantrobot 4y ago
- moring 4y agoThat only means you have to distinguish by frequency and magnitude of quirks, and languages are very different then.
- ahofmann 4y agoThis is great news! Everyone who doesn't use PHP increases my hourly rate ;-)
- skrowl 4y ago[dead]
- omnibrain 4y agoYou are going to love https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/ https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/
- JW_00000 4y agoBy the way, in JavaScript: > let a = "0e462097431906509019562988736854"; // = md5("240610708") > let b = "0e830400451993494058024219903391"; // = md5("QNKCDZO") > let c = 0; > a == c true > b == c true but > a == b false (Of course, also solved by using ===)
- deleted 4y ago[deleted]
- pdpi 4y agoOh that is nasty. For anybody who cares: `a == c` coerces `a` into a number . `a` is a hexadecimal string that happens to have only one non-numeric 'digit', and that digit happens to be an 'e'. This triggers parsing as scientific notation. For both `a` and `b`, the only digit in the significand is 0, so the result is 0.
- hn_throwaway_99 4y agoI think it's a lot less nasty than the Python version. At least the JS version only gives you a weird answer if you're comparing a number to a string, while Python will automatically do a weird type conversion even if you're just comparing two "number looking strings" to begin with. Edit: I'm an idiot, thanks for the responses for correcting me, was thinking PHP but had Python on the brain I guess. Original bad edit: I was downvoted pretty hard, but I think I was just unclear. In Python, doing md5("foo") == md5("bar") returns true because Python converts both strings to numbers first because they have an e in them. That is, in my opinion, Python is doing an unnecessary conversion there that JS doesn't do in that case. See https://stackoverflow.com/questions/12598407/why-does-php-convert-a-string-with-the-letter-e-into-a-number https://stackoverflow.com/questions/12598407/why-does-php-co...
- cochne 4y agoPython doesn’t do any automatic type conversion for strings
- throwaway81523 4y agoWAT
- rollulus 4y agoLearnt about this 8 years ago when it was on top of HN: https://news.ycombinator.com/item?id=9484757 https://news.ycombinator.com/item?id=9484757
- avsteele 4y agoMan, this takes me back. I remember trying to help a friend with their PHP assignment in college (~2001). I had only ever worked with C, C++, and java in my classes and I just remember being was confused as hell since I couldn't understand why PHP worked the way it did.
- spyremeown 4y agoThe "triple equal" or "really really equal" or whatever we call it is the stupidest thing a language can have. If your language needs something like that, it already failed* somewhere along the way. * Which doesn't imply it's not useful.
- foobarbecue 4y agoIn python, I find it pretty intuitive. It's very convenient to have "do these two objects have the same contents" vs "are these two the same object at the same memory address." Other language design options are to use pointers / references, be strongly typed or be functional. Seems like you're saying all weakly-typed object-oriented languages are failures?
- roflyear 4y agoI don't think "===" in PHP is the same as "is" in Python, though? Identity is important (why would you want to check for 0, None, "", etc.. in separate cases, unless you want to?) but I don't think that is what "===" in PHP.
- flir 4y agoPHP exists in a place where everything's a string (HTTPland). Type coercion makes more sense there.
- foobarbecue 4y agoOh you're right. I actually was thinking python "is" was written "===" because I got confused with javascript, yikes. And I don't speak PHP. Anyway yeah being able to compare identity and equivalence separately seems important.
- hn_throwaway_99 4y agoPretty hard disagree. There are fundamentally different definitions of "equal", and pretty much all languages I can think of support that (e.g. reference equality vs. Object.equals in Java). You can argue that triple equals is a confusing syntax, but the underlying problem is inherent in the world we live in.