Difference Between == (equal) and === (comparison) Operators In Php

Difference Between == (equal) and === (comparison) Operators In Php

Equal(==) and Identical(===) operators are two of the most used operators in Php. While they are similar, there is still a major difference which is that '==' is used to check if the values of two operands are equal or not. While '===' is used to check both values and operand type.

Explanation using Examples

"==" EQUAL

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

The above code will print YES because the values of both operands are equal. Whereby running the code below

"===" Identical

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

The result is going to be No. The reason is that although the value of both operands is the same their types are different, the "10" is a string due to the quote while the 10 is an integer.

But changing the code to the following

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

It would echo YES. The reason is that we changed the operand of the right to a string which makes both operands the same as strings. Now both values and operands have the same value and operand.

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

Please feel free to use the comments form below if you have any questions or need more explanation on anything.