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.
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
Waxspacehosting
Member
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
- Do you really know that PHP is strange programming language34 votes
- Yes55.88%
- No44.12%
Comments
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
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
Providerdeveloper?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
Go ahead and read; https://phpbestpractices.org/ & http://www.phptherightway.com/
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.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?
Yep, his other 3 posts are not much better.
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.
Certainly, I've less posts. I'm trying to communicate with community by sharing stuff.
True said, totally agreed.
I actually didn't know about that (I mean why PHP believes they are equal)
But I've always used
===
anywaySo 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?
Should've used the mullet operator
Does it get tiring when you spend the whole day farming?
He never paid me.