Howdy, Stranger!

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

Sign In with OpenID
Advertise on LowEndTalk.com

In this Discussion

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 14 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

Tagged:

Comments

  • FlamesRunnerFlamesRunner Member
    edited February 14

    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

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

    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?

    Netcup DE KVM: 1vC 1GB - 18,88€ or 2 Core 3GB 240GB - 49,88€ yearly /w 5€ off: 36nc15110099285 - 36nc15110099288
    UltraVPS.eu KVM in US/NL/DE, 1BLACK FRIDAY: 1GB 20€ or 2GB 40€ yearly or cheap 750G / 2TB storage offers

    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

  • imokimok Member

    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, Provider

    postcountbaitmuch?

    Inception Hosting Limited - NL, UK, Phoenix AZ USA, KVM and OpenVZ | NEW PURE NVME SSD OpenVZ UK

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

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

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

    Thanked by 1imok
  • WSSWSS Member
    edited February 15
    <?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 purchase a VPS at any CC location, because of their shitty business and the fact they STILL DON'T HAVE IPv6!!" - nunim, Sept 10, 2013

  • 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 (SSD10 for 10% off, 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

    Netcup DE KVM: 1vC 1GB - 18,88€ or 2 Core 3GB 240GB - 49,88€ yearly /w 5€ off: 36nc15110099285 - 36nc15110099288
    UltraVPS.eu KVM in US/NL/DE, 1BLACK FRIDAY: 1GB 20€ or 2GB 40€ yearly or cheap 750G / 2TB storage offers

  • imokimok Member

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

  • 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
  • Should've used the mullet operator

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

  • WSSWSS Member

    @ricardo said: Should've used the mullet operator

    He never paid me.

    "I won't purchase a VPS at any CC location, because of their shitty business and the fact they STILL DON'T HAVE IPv6!!" - nunim, Sept 10, 2013

Sign In or Register to comment.