== ( or ===)

A place for general chat, unrelated to web development, or loosely related.

== ( or ===)

Postby extra » Tue Mar 09, 2010 12:30 pm

Hi.. This is a stupid question but why not give it a try?

is[php]<?php
if($var1 == $var2){}
?>[/php]
the same as[php]<?php
if($var2 == $var1){}
?>[/php]
?

also, what is recommended to use, == or === ?
Karma++; me if I helped! :roll:
GameZ4Free::playGames();

I was Hyper.. Now I'm extra.
User avatar
extra
Top Contributor
 
Posts: 1020
Joined: Fri Nov 06, 2009 7:08 pm
Online: 7d 58m 10s
Karma: 18

Re: == ( or ===)

Advertisment

Advertisment
 

Re: == ( or ===)

Postby Tino » Tue Mar 09, 2010 3:10 pm

Well, it is, but if you want to see if $var1 is equal to something, it doesn't make sense to make it the right-hand operand. It's not semantically correct.

As far as I know, it doesn't matter if you use == or ===. I guess === is a little bit more secure or something, otherwise I don't really see how it could be significantly superior.
The below statement is false.
The above statement is true.

CodeCanyon
There are some pretty cool items over here, you should check them out.

There are 10 kinds of people, those who understand binary, and those who do not.
User avatar
Tino
Moderator
 
Posts: 1075
Joined: Wed Apr 22, 2009 1:28 pm
Location: The Netherlands
Online: 5d 22h 11m 28s
Karma: 12

Re: == ( or ===)

Postby wide_load » Tue Mar 09, 2010 6:07 pm

Tino wrote:Well, it is, but if you want to see if $var1 is equal to something, it doesn't make sense to make it the right-hand operand. It's not semantically correct.

As far as I know, it doesn't matter if you use == or ===. I guess === is a little bit more secure or something, otherwise I don't really see how it could be significantly superior.


no

not at all

=== also checks the data type for example
[php]if (true == 1){
// this would be executed
}

if (true === 1){
// this would not be executed
}[/php]

nothing to do with security at all.

=== is technically faster, as == converts each side to all of the possible data types for comparison where as === just compares them.

finally the order in the if statement does not matter in the slightest as if x = y then that also means that y = x
Image
User avatar
wide_load
Top Contributor
 
Posts: 5617
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 22h 25s
Karma: 45

Re: == ( or ===)

Postby Tino » Tue Mar 09, 2010 7:50 pm

=== also checks the data type for example


Yes, I knew that. I just didn't know if there was any particular reason for its existence, so just made a random guess that it had something to do with security :3
The below statement is false.
The above statement is true.

CodeCanyon
There are some pretty cool items over here, you should check them out.

There are 10 kinds of people, those who understand binary, and those who do not.
User avatar
Tino
Moderator
 
Posts: 1075
Joined: Wed Apr 22, 2009 1:28 pm
Location: The Netherlands
Online: 5d 22h 11m 28s
Karma: 12

Re: == ( or ===)

Postby wide_load » Wed Mar 10, 2010 6:18 pm

Tino wrote:
=== also checks the data type for example


Yes, I knew that. I just didn't know if there was any particular reason for its existence, so just made a random guess that it had something to do with security :3


it exists so that you can also check the data type
Image
User avatar
wide_load
Top Contributor
 
Posts: 5617
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 22h 25s
Karma: 45

Re: == ( or ===)

Postby Cags » Thu Mar 11, 2010 11:56 am

The main use is generally when checking a return value from a function since it can tell the difference between FALSE and 0.
"I don't suffer from insanity, I enjoy every minute of it."
- Pete
CodeCanyon - Cheap, High Quality, Ready Made Scripts.
User avatar
Cags
Moderator
 
Posts: 1863
Joined: Fri May 22, 2009 9:35 am
Location: Purgatory
Online: 3d 1h 51m 14s
Karma: 9

Re: == ( or ===)

Postby Pagweb » Sun Apr 04, 2010 7:36 pm

The === checks two things, that the values and data types are the same.
Example:
5 == "5" is true
5 === "5" is false because they are not the same data type.
Marcos López C.
http://www.marsow.com

Adobe Certified Associate
Dreamweaver CS4
Photoshop CS4
Flash CS4
- - - - - - - - - - - - - -
Dreamweaver CS3
Photoshop CS3
User avatar
Pagweb
 
Posts: 4
Joined: Tue Mar 30, 2010 3:04 pm
Location: Florida, USA
Online: 4h 9m 33s
Karma: 0


Return to General Chat

Who's online?

Users browsing this forum: No registered users and 1 guest