Support Forums
Why do we use == rather than === - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21)
+---- Thread: Why do we use == rather than === (/showthread.php?tid=12676)



Why do we use == rather than === - Orgy - 10-11-2010

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?


RE: Why do we use == rather than === - Disease - 10-11-2010

(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 (===).


RE: Why do we use == rather than === - phire nuk3r - 10-11-2010

Because it would have to be exact 100% For example it would even not return true if the case was different etc.


RE: Why do we use == rather than === - Orgy - 10-11-2010

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 "=="?


RE: Why do we use == rather than === - phire nuk3r - 10-11-2010

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


RE: Why do we use == rather than === - Orgy - 10-11-2010

(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"


RE: Why do we use == rather than === - Disease - 10-11-2010

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


RE: Why do we use == rather than === - Orgy - 10-11-2010

(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


RE: Why do we use == rather than === - Chimi - 12-06-2010

I didn't even know the "===" function existed. Thanks for letting me know what it means.


RE: Why do we use == rather than === - Orgy - 12-09-2010

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