Diference between == and === in php?

Diference between == and === in php?

 Vaibhav

View (914)

When comparing values in PHP for equality you can use either the == operator or the === operator. What’s the difference between both the operator? Well, it’s quite simple. The '==' (i.e equal) operator just checks to see if the left and right values are equal. But, the === (i.e. identical) operator (note the extra '=') actually checks to see if the left and right values are equal, and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).

Let us see the explaination using some examples:

'==' (Equal) :
 


if("100" == 100) echo "YES";
 else           echo "NO";

 

The code above will print "YES". The reason is that the values of the operands are equal. Whereas when we run the example code below:

'===' (Identical):


if("100" === 100) echo "YES";
 else            echo "NO";

 

The result we get is "NO". The reason is that although values of both operands are same their types are different, "100" (with quotes) is a string while 100 (w/o quotes) is an integer. But if we change the code above to the following:


if("22" === (string)22) echo "YES";
 else           echo "NO";

 

Then, the result will be "YES".

Note that we changed the type of right operand to a string which is the same as the left operand (i.e. string). Now, the types and values of both left and right operands are the same hence both operands are identical.

So, whenever you want to compare the values as well as the types of operands you'll use '===' otherwise you use '=='.

About the author

Most Read

The new kid in the block: Android Noughat

Google has been following a similar pattern with its Android naming process....

Watch Security Cams in the open using Google dorks

Are you aware that there is no such thing on the internet as, “Hiding in...

How to create Splash Screen in Android

Android splash screen are normally used to show user some kind of progress...

Reasons that prove BIG DATA is the next Big thing!

According to analysts, the big data market has been predicted to be USD $50...

PHP vs ASP.Net (Championship)

Here we are about to start with the boxing match between two champions.One side...
X
Register Here
 
By signing up , you agree to our Terms of Service and Privacy Policy.
 Login With
Facebook
Facebook
Google
Google
X
Log In Here
 
Remember me Forgot password?
 Login With
Facebook
Facebook
Google
Google
X
Reset Password
 
X

Confirmation Message

Yes No
X
Forgot Password
 
Do you want to Login ?