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

Last updated 1 year, 9 months ago | 254 views 75     5

"==" and "==="

The two operators known as comparison operator. "==" (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". becouse both operands are same but their types are different, "123 " (with quotes) is a string whereas 123  (without quotes) is an integer.