Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do we use == rather than ===
#1
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?
[Image: TYzKF.png]
Reply
#2
(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 (===).
Ho, ho, ho! Well, if it isn't fat stinking billy goat Billy Boy in poison!
How art thou, thou globby bottle of cheap, stinking chip oil?
Come and get one in the yarbles, if ya have any yarbles, you eunuch jelly thou!
Reply
#3
Because it would have to be exact 100% For example it would even not return true if the case was different etc.
[Image: s1.jpg]
Reply
#4
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 "=="?
[Image: TYzKF.png]
Reply
#5
(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.
[Image: s1.jpg]
Reply
#6
(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"
[Image: TYzKF.png]
Reply
#7
(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.
Ho, ho, ho! Well, if it isn't fat stinking billy goat Billy Boy in poison!
How art thou, thou globby bottle of cheap, stinking chip oil?
Come and get one in the yarbles, if ya have any yarbles, you eunuch jelly thou!
Reply
#8
(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
[Image: TYzKF.png]
Reply
#9
I didn't even know the "===" function existed. Thanks for letting me know what it means.
Reply
#10
(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.
[Image: TYzKF.png]
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)