Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Strange behavior of PHP Comparing operator
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Strange behavior of PHP Comparing operator

edited February 2017 in General

Recently, I've started working on some OpenStack API. I'm sharing this coding experience with you.

Take you time and think following PHP code is wrong or right ?

<?php var_dump(md5('240610708') == md5('QNKCDZO')); ?>

You can run this code here

I was expecting the answer false, however the PHP says the above mentioned value is equal.

The correct way to write the PHP comparing -

<?php var_dump(md5('240610708') === md5('QNKCDZO')); ?>

So, today I found for comparing always === in PHP code.

PHP is strange programming language
  1. Do you really know that PHP is strange programming language34 votes
    1. Yes
      55.88%
    2. No
      44.12%

Comments

  • FlamesRunnerFlamesRunner Member
    edited February 2017

    The == operator compares two things regardless of the characters within it are capitalized or not.

    On the other hand, === only allows absolutely equal conditions.

    It's not strange, it was built in.


    Edit: Please, just please don't use md5

    Thanked by 3netomx WSS GCat
  • so you're telling you want to write an openstack API in PHP or whatever but don't even know about basic comparison operators?

    Do you really know Waxspacehosting is a strange wannabe Provider developer?

    Thanked by 1GCat
  • Yes, it is common mistakes for a weak typed language to misuse the equal operator, we need to be extra carefull when comparing variables when playing with such languages

  • You are comparing strings. You should be doing:

    strcmp(md5('240610708'), md5('QNKCDZO')) == 0

    In case someone else is asking why, you have the explanation here http://stackoverflow.com/questions/22140204/why-md5240610708-is-equal-to-md5qnkcdzo

    Thanked by 3netomx dodedodo yomero
  • AnthonySmithAnthonySmith Member, Patron Provider

    postcountbaitmuch?

  • @Waxspacehosting === compares the type as well, I'd go into details but you'd have figured it out with a quick google search. Posting this only makes you look like a newbie even though you mentioned fancy words like API and md5.

    yohanip said: Yes, it is common mistakes for a weak typed language to misuse the equal operator, we need to be extra carefull when comparing variables when playing with such languages

    That makes no sense, this is also the nature of comparison operators in Javascript/nodejs as well. What would you consider a 'strong' language with better implementation of comparison operators?

  • @AnthonySmith said:
    postcountbaitmuch?

    Yep, his other 3 posts are not much better.

  • FlamesRunner said: The == operator compares two things regardless of the characters within it are capitalized or not.

    After few min of research on the Google, I found that == operator is also consider the Scientific number.

    The Md5('240610708') = 0e462097431906509019562988736854
    The Md5('240610708') = 0e830400451993494058024219903391
    As you can see the Md5('240610708') hash is started '0e' and Md5('240610708') PHP is considering as scientific number which is equivalent to the Zero.

  • @teamacc said:

    @AnthonySmith said:
    postcountbaitmuch?

    Yep, his other 3 posts are not much better.

    Certainly, I've less posts. I'm trying to communicate with community by sharing stuff.

  • @srvrpro said:
    @Waxspacehosting === compares the type as well, I'd go into details but you'd have figured it out with a quick google search. Posting this only makes you look like a newbie even though you mentioned fancy words like API and md5.

    yohanip said: Yes, it is common mistakes for a weak typed language to misuse the equal operator, we need to be extra carefull when comparing variables when playing with such languages

    That makes no sense, this is also the nature of comparison operators in Javascript/nodejs as well. What would you consider a 'strong' language with better implementation of comparison operators?

    True said, totally agreed.

    Thanked by 1imok
  • WSSWSS Member
    edited February 2017
    <?php
    // ponderous man, php is fuckin' ponderous!
    $thread=array();
    $thread['dumb'] = TRUE;
    $thread['reallydumb'] = 1;
    $response=($thread['dumb'] == $thread['realdumb'] ? "Really dumb" : "Not so dumb");
    echo "This thread is $response\n";
    ?>
    
  • I actually didn't know about that (I mean why PHP believes they are equal)

    But I've always used === anyway

  • hostnoob said: I mean why PHP believes they are equal

    imok said: In case someone else is asking why, you have the explanation here http://stackoverflow.com/questions/22140204/why-md5240610708-is-equal-to-md5qnkcdzo

  • @Waxspacehosting said:

    FlamesRunner said: The == operator compares two things regardless of the characters within it are capitalized or not.

    After few min of research on the Google, I found that == operator is also consider the Scientific number.

    The Md5('240610708') = 0e462097431906509019562988736854
    The Md5('240610708') = 0e830400451993494058024219903391
    As you can see the Md5('240610708') hash is started '0e' and Md5('240610708') PHP is considering as scientific number which is equivalent to the Zero.

    So you found this operator thing (including the question and the answer) after a Google search, and you did another search to found the answer again?

  • @imok said:

    @Waxspacehosting said:

    FlamesRunner said: The == operator compares two things regardless of the characters within it are capitalized or not.

    After few min of research on the Google, I found that == operator is also consider the Scientific number.

    The Md5('240610708') = 0e462097431906509019562988736854
    The Md5('240610708') = 0e830400451993494058024219903391
    As you can see the Md5('240610708') hash is started '0e' and Md5('240610708') PHP is considering as scientific number which is equivalent to the Zero.

    So you found this operator thing (including the question and the answer) after a Google search, and you did another search to found the answer again?

    This is leading to off-topic:

    I consider this is your predictions. Just to tell you I've shared my experience, so that other members should not do the same mistake.

  • You experienced an exact Stack Overflow question from years ago, complete with the exact same values?

    Thanked by 3ricardo Four20 bugrakoc
  • Should've used the mullet operator

  • Does it get tiring when you spend the whole day farming?

  • @ricardo said:
    Should've used the mullet operator

    He never paid me.

Sign In or Register to comment.