What is the difference between == and === Operators in PHP?
PHP equal operator (==)
and idential operator (===)
are Relational or Comparison Operators in PHP langauge.
The only difference is that === o
perator matches the values along with Data types. While ==
operator only match values but not Data types.
Example to see difference between both operators
<?php
$num_1 = 12;
$num_2 = 12.00;
if ($num_1 == $num_2) {
echo "Value matched using ==";
} else {
echo "Value is not matched using ==";
}
echo "<br> Now we will check using === operator <br>";
if ($num_1 === $num_2) {
echo "Value is matched using ===";
} else {
echo "Value is not matched using ===";
}
?>
We have compared the two variable one by one by using == and === Operator, we get the following result.
Tutorials Class - Output Window
Value matched using ==
Now we will check using === operator
Value is not matched using ===
You will see that value is matched if used the equal operator '==
‘ and not matched if used identical operator '==='
because data type is not same for both numbers.
Therefore, if we need to match both values strictly with datatypes, '==='
operator will be used else '=='
will be used in PHP.
Learn more about the similar topics:
Tutorials |
---|
PHP Operators |
Exercises & Assignments |
---|
Write a PHP program to add two numbers |
Write a PHP program to check whether a number is positive, negative or zero |
Interview Questions & Answers |
---|
What is the difference between == and === Operators in PHP? |