Support Forums

Full Version: Why do we use == rather than ===
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
If "===" means "exactly equal", then why don't we use it all the time. Well, with the exception of checking if something is true (true, >0, etc)

But I rarely see anyone ever use "===" for comparing strings. Why not? Wouldn't that be more accurate?
(10-11-2010, 06:39 AM)Orgy Wrote: [ -> ]If "===" means "exactly equal", then why don't we use it all the time. Well, with the exception of checking if something is true (true, >0, etc)

But I rarely see anyone ever use "===" for comparing strings. Why not? Wouldn't that be more accurate?

It's more accurate if you need it to be. Most people utilize PHP's ability to blur data types. You can compare "4" and 4 and they'd be equal. This is useful in certain situations but dangerous in others because people get complacent. If you know what you're comparing will always be a string and it always needs to be a string, then use the identical operator (===).
Because it would have to be exact 100% For example it would even not return true if the case was different etc.
I see, Disease. So then would 1 === "1"?
(10-11-2010, 06:43 AM)phire nuk3r Wrote: [ -> ]Because it would have to be exact 100% For example it would even not return true if the case was different etc.

Correct me if I'm wrong, but if the case were different, wouldn't it still evaluate to false, even if you only used "=="?
(10-11-2010, 06:45 AM)Orgy Wrote: [ -> ]I see, Disease. So then would 1 === "1"?

Correct me if I'm wrong, but if the case were different, wouldn't it still evaluate to false, even if you only used "=="?

Yes you are wrong.
(10-11-2010, 06:53 AM)phire nuk3r Wrote: [ -> ]Yes you are wrong.

I've just tested it. You're right. I swear I've needed to use strtolower() before for something to make sure it was right. Huh.

EDIT: Actually, I just checked my code, I forgot a "=", lol. Turns out I'm right.

EDIT AGAIN: You know, I'm not sure what I forgot. It's confused the hell out of me now.

Here is my code:

PHP Code:
if ("lol" == "LOL")
{
    echo 
"lol == LOL";
}
else
{
    echo 
"lol != LOL";


It echos "lol != LOL"
(10-11-2010, 06:43 AM)phire nuk3r Wrote: [ -> ]Because it would have to be exact 100% For example it would even not return true if the case was different etc.

Nor would the equals (==) operator. "Hey" and "hey" are not equal; 4 and "4" are equal but not identical. The equals operator does not eliminate case sensitivity. In order to do that you'd need to use a function such as strcasecmp () or strtolower ()/strtoupper (). The difference being that strcmp ()/strcasecmp () are binary safe and the equals/identical operators are not.

And Orgy, no, (1 === "1") would return false. They are equal (1 == "1") but they are not identical due to the difference in data type. The LHV is an integer and the RHV is a string literal.
(10-11-2010, 09:12 AM)Disease Wrote: [ -> ]Nor would the equals (==) operator. "Hey" and "hey" are not equal; 4 and "4" are equal but not identical. The equals operator does not eliminate case sensitivity. In order to do that you'd need to use a function such as strcasecmp () or strtolower ()/strtoupper (). The difference being that strcmp ()/strcasecmp () are binary safe and the equals/identical operators are not.

And Orgy, no, (1 === "1") would return false. They are equal (1 == "1") but they are not identical due to the difference in data type. The LHV is an integer and the RHV is a string literal.

Yeah I tested all of that shortly after asking, and found that all out. Nice to know
I didn't even know the "===" function existed. Thanks for letting me know what it means.
(12-06-2010, 01:29 AM).Shannon Wrote: [ -> ]I didn't even know the "===" function existed. Thanks for letting me know what it means.

Well, it isn't a function. It's an operator.