Live data from Hacker News

PHP: md5('240610708') == md5('QNKCDZO')

3v4l.org

11–20 of 185 posts

Re: PHP: md5('240610708') == md5('QNKCDZO')

#11

I'm not exactly clear on how PHP == works, but you can see the MD5 for yourself: $ echo -n 240610708 | md5sum 0e462097431906509019562988736854 - $ echo -n QNKCDZO | md5sum 0e830400451993494058024219903391 - $ echo -n aabg7XSs | md5sum 0e087386482136013740957780965295 - All of them start with 0e, which makes me think that they're being parsed as floats and getting converted to 0.0. This is why "magic" operators like =…

You're right:

% php -r 'var_dump("0e1" == "0e2");' bool(true)

Re: PHP: md5('240610708') == md5('QNKCDZO')

#13
Reminds me on bash, where I also have to prefix values to compare with x, to be able to handle empty vars.

    if [ x$1 == x$2 ];
But automatic string to float conversion is just crazy, esp. in comparison context. Perl, which is equally soft, has at least numerical and string comparison operators.

    $ perl -e'print "0e462097431906509019562988736854" ==
                    "0e830400451993494058024219903391"'
    1
    $ perl -e'print "0e462097431906509019562988736854" eq
                    "0e830400451993494058024219903391"'
So the solution is to use === which does not compare references with strings but the values, or the strcmp function. And refrain from using == with strings at all. '0XAB' == '0xab' is true. Comparing any string to 0 with == will return true.

Re: PHP: md5('240610708') == md5('QNKCDZO')

#14
PHP's type coercion is nothing like I have every seen in any other language. Its horrendously messy, ugly and completely inexcusable. Strings type-casted to integers are 0. Seriously? Take a look at this,

> $arr = array(0, "was", "invented", "in", "india");

> var_dump( in_array("Hello", $arr ) );

and yeah it is TRUE because "Hello" got coerced to 0. I blogged about a major bug, I faced, in PHP, where column name "10th_grade" was being type-casted to "10" failing the "bindParam" [1]. Even if they have to continue this "feature" because of backwards compatibility, the least they could have done was NOT to use it in the newer functions but no, even they have this stupid "type juggling".

[1]: http://coffeecoder.net/blog/my-perfect-reason-avoid-php-type...

Re: PHP: md5('240610708') == md5('QNKCDZO')

#15
post #13

Reminds me on bash, where I also have to prefix values to compare with x, to be able to handle empty vars. if [ x$1 == x$2 ]; But automatic string to float conversion is just crazy, esp. in comparison context. Perl, which is equally soft, has at least numerical and string comparison operators. $ perl -e'print "0e462097431906509019562988736854" == "0e830400451993494058024219903391"' 1 $ perl -e'print "0e46209743190650…

I'm not sure why does this crazy "x" prefix tale still continue. You can simply quote them instead. Especially if you use bash and not some other sh-compatible shell:

    if [ "$1" == "$2" ];
will work just fine.

If you need all sh compatibility, it should be test for "x$1" anyway (still quoted).

Re: PHP: md5('240610708') == md5('QNKCDZO')

#16

I'm not exactly clear on how PHP == works, but you can see the MD5 for yourself: $ echo -n 240610708 | md5sum 0e462097431906509019562988736854 - $ echo -n QNKCDZO | md5sum 0e830400451993494058024219903391 - $ echo -n aabg7XSs | md5sum 0e087386482136013740957780965295 - All of them start with 0e, which makes me think that they're being parsed as floats and getting converted to 0.0. This is why "magic" operators like =…

This, combined with the fact that you can increment strings gives some 'interesting' results:

    $a = "2d9"; 
    $a++; 
    echo $a . "\n"; 
    $a++; 
    echo $a . "\n"; 
Output

    2e0
    3

Re: PHP: md5('240610708') == md5('QNKCDZO')

#17
post #13

Reminds me on bash, where I also have to prefix values to compare with x, to be able to handle empty vars. if [ x$1 == x$2 ]; But automatic string to float conversion is just crazy, esp. in comparison context. Perl, which is equally soft, has at least numerical and string comparison operators. $ perl -e'print "0e462097431906509019562988736854" == "0e830400451993494058024219903391"' 1 $ perl -e'print "0e46209743190650…

Actually, you don't prefix with “x” to handle empty vars, but special characters, as Stephane Chazelas recently reminded: http://www.zsh.org/mla/workers/2015/msg00797.html

Re: PHP: md5('240610708') == md5('QNKCDZO')

#18
post #13

Reminds me on bash, where I also have to prefix values to compare with x, to be able to handle empty vars. if [ x$1 == x$2 ]; But automatic string to float conversion is just crazy, esp. in comparison context. Perl, which is equally soft, has at least numerical and string comparison operators. $ perl -e'print "0e462097431906509019562988736854" == "0e830400451993494058024219903391"' 1 $ perl -e'print "0e46209743190650…

I'm not sure why does this crazy "x" prefix tale still continue. You can simply quote them instead. Especially if you use bash and not some other sh-compatible shell: if [ "$1" == "$2" ]; will work just fine. If you need all sh compatibility, it should be test for "x$1" anyway (still quoted).

I think you meant “=”, not “==” (though the latter would work with bash).

Re: PHP: md5('240610708') == md5('QNKCDZO')

#20

Earlier quoted context omitted.

I'm not sure why does this crazy "x" prefix tale still continue. You can simply quote them instead. Especially if you use bash and not some other sh-compatible shell: if [ "$1" == "$2" ]; will work just fine. If you need all sh compatibility, it should be test for "x$1" anyway (still quoted).

I think you meant “=”, not “==” (though the latter would work with bash).

Well, either in the example. Parent was saying "Reminds me on bash"

For sh version, I'd go with super-safe:

    if test "x$1" = "x$2"
Post reply on HN