== Operator
The ‘==’ is not used for assignment purpose. It is only used to compare the values of two given operands, not their data types. This operator is used to check if the given values are equal or not. If both the values are equal, it returns true, otherwise it returns false.
In below example, the string “34” is automatically converted to the integer 34 for the comparison, and since both values are numerically equal, the condition is true and “Equal” is echoed.
Syntax:
operand1 == operand2
Example: Implementation to showcase the use of ‘==’ operator.
<?php
$a = 34;
$b = 34;
// When both have equal
// value and data type
if ($a == $b) {
echo "Equal";
} else {
echo "Not Equal";
}
echo "\n";
// When both have equal value
// but different data type
if ("34" == 34) {
echo "Equal";
} else {
echo "Not Equal";
}
?>
Output
Equal Equal
Difference between the (=), (==), and (===) operators in PHP
In PHP, the ‘=’ operator is used for assignment, while the ‘==’ operator is used for loose equality comparison, meaning it checks if two values are equal without considering their data types. On the other hand, the ‘===’ operator is used for strict equality comparison, meaning it checks if two values are equal and of the same data type.
Contact Us