Diference between == and === in php?

Diference between == and === in php?

 Vaibhav

View (472)

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

Reasons that prove BIG DATA is the next Big thing!

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

How to create Splash Screen in Android

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

Watch Security Cams in the open using Google dorks

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

bootable pendrive with easy steps

If you want to install window using your pendrive then this post is for...

12 Creative Ways to Promote Your App for Free

Most people think that successful mobile apps get magically downloaded millions...
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 ?