what is php identical (===) operator

If you are using the == (equal ) operator, or any other comparison operator which uses loosely comparison such as !=, <> or ==, you always have to look at the context to see what, where and why something gets converted to understand what is going on.
Two of the many comparison operators used by PHP are ‘==’ (equal) and ‘===’ (identical). The difference between the two is that ‘==’ (equal) should be used to check if the values of the two operands are equal or not. On the other hand, ‘===’ (identical) checks the values as well as the type of operands.

‘==’ (Equal):

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

The code above will print “YES”. The reason is that the values of the operands are equal.

‘===’ (Identical):

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

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

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

Then, the result will be “YES”. Notice 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.

Leave a Reply

Your email address will not be published. Required fields are marked *