Strange behavior of PHP Comparing operator
New on LowEndTalk? Please read our 'Community Rules' by clicking on it in the right menu!

Strange behavior of PHP Comparing operator

WaxspacehostingWaxspacehosting Member, Provider
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%

Waxspace.in Pune Location VPS - 28 USD/Mo 2GB RAM, 45 GB Storage - Plan will be Launched Soon at Waxspace

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

    wget https://s.flamz.pw/dl/bench.sh && bash bench.sh

    curl https://s.flamz.pw/analytics/bench/stats.php

  • 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

    UltraVPS.eu KVM in US/UK/NL/DE: 15% off first 6 month and cheap 750G / 2TB storage offers
    Netcup KVM: 2GB 40TB BW - 16,14€ 6m or 2 dedCore 6GB 320GB - 78,88€ 12m /w 5€ off: 36nc15279180197 | 36nc15292244387

  • 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

    Janitor @ Netflix

  • @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.

    Yo mama so fat each of her butt-cheeks has its own /8.
    Thanked by (1): [Deleted User].

  • WaxspacehostingWaxspacehosting Member, Provider

    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.

    Waxspace.in Pune Location VPS - 28 USD/Mo 2GB RAM, 45 GB Storage - Plan will be Launched Soon at Waxspace

  • WaxspacehostingWaxspacehosting Member, Provider

    @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.

    Waxspace.in Pune Location VPS - 28 USD/Mo 2GB RAM, 45 GB Storage - Plan will be Launched Soon at Waxspace

  • WaxspacehostingWaxspacehosting Member, Provider

    @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

    Waxspace.in Pune Location VPS - 28 USD/Mo 2GB RAM, 45 GB Storage - Plan will be Launched Soon at Waxspace

  • 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 won't be back until @bsdguy is released.

  • I actually didn't know about that (I mean why PHP believes they are equal)

    But I've always used === anyway

    Favourite host in general: Ramnode (affiliate link)
    Favourite host for hourly billing/custom ISOs: Vultr (affiliate link)

  • 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

    UltraVPS.eu KVM in US/UK/NL/DE: 15% off first 6 month and cheap 750G / 2TB storage offers
    Netcup KVM: 2GB 40TB BW - 16,14€ 6m or 2 dedCore 6GB 320GB - 78,88€ 12m /w 5€ off: 36nc15279180197 | 36nc15292244387

  • @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?

    Janitor @ Netflix

  • WaxspacehostingWaxspacehosting Member, Provider

    @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.

    Waxspace.in Pune Location VPS - 28 USD/Mo 2GB RAM, 45 GB Storage - Plan will be Launched Soon at Waxspace

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

    Thanked by 3ricardo Four20 bugrakoc
  • Does it get tiring when you spend the whole day farming?

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

    He never paid me.

    I won't be back until @bsdguy is released.

Sign In or Register to comment.