➤ Comparison operators compare two values (numbers or strings).
Comparison operators are used inside conditional statements, and evaluate to either TRUE or FALSE.
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
For instance:
// Equal
$x = 50;
$y = "50";
var_dump($x == $y); // returns TRUE because values are equal
echo "\n";
// Identical
$x = 50; // integer type
$y = "50"; // string type
var_dump($x === $y); // returns false because types are not equal
echo "\n";
// Not equal
$x = 50;
$y = "50";
var_dump($x != $y); // returns false because values are equal
echo "\n";
// Not equal. Both operators (!= / <>) give the same output. The only difference is that '<>' is in line with the ISO standard while '!= ' does not follow ISO standard.
$x = 50;
$y = "50";
var_dump($x <> $y); // returns false because values are equal
echo "\n";
// Not identical
$x = 50;
$y = "50";
var_dump($x !== $y); // returns true because types are not equal
echo "\n";
// Greater than
$x = 100;
$y = 50;
var_dump($x > $y); // returns true because $x is greater than $y
echo "\n";
// Less than
$x = 10;
$y = 50;
var_dump($x < $y); // returns true because $x is less than $y
echo "\n";
// Greater than or equal to
$x = 10;
$y = 10;
var_dump($x >= $y); // returns true because $x is greater than or equal to $y
echo "\n";
// Less than or equal to
$x = 10;
$y = 10;
var_dump($x <= $y); // returns true because $x is less than or equal to $y
echo "\n";