What is difference between == and === operator ?

Last updated 5 years ago | 1282 views 75     5

Tags:- PHP

PHP | What is difference between == and === operator?

"==" and "==="

The two operators are known as comparison operators. "==" (i.e. equal)  and === (i.e. identical) both are use to check values are same or not. The difference between the two is that '==' should be used to check if the values of the two operands are equal or not whereas, '===' checks the values as well as the type of operands.

 

 == (equal):

<?php
if("123 " == 123) {
    echo "YES";
} else {
    echo "NO";
}
?>

//Output: Yes

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

=== (Identical):

Whereas when we run the example code below:

<?php
if("123 " === 123 ) {
    echo "YES";
} else {
    echo "NO";
}
?>

//Output: No

The result we get is "NO". because both operands are the same but their types are different, "123 " (with quotes) is a string whereas 123  (without quotes) is an integer.