Diference between == and === in php?
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:
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 '=='.