PHP Comparison Operators

Rumman Ansari   Software Engineer   2020-02-06   5397 Share
☰ Table of Contents

Table of Content:


The PHP comparison operators are used to compare two values (number or string):

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
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.

Example: == Equal

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;  
$y = "100";

var_dump($x == $y); // returns true because values are equal
?>  

</body>
</html>


Output:

This will produce the following result



bool(true)


Example: === Identical

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 200;  
$y = "100";

var_dump($x === $y); // returns false because types are not equal
?>  

</body>
</html>



Output:

This will produce the following result



bool(false)


Example: != Not equal

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 20;  
$y = "20";

var_dump($x != $y); // returns false because values are equal
?>  

</body>
</html>


Output:

This will produce the following result



bool(false)


Example: <> Not equal

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 20;  
$y = "20";

var_dump($x <> $y); // returns false because values are equal
?>  

</body>
</html>



Output:

This will produce the following result



bool(false)


Example: !== Not identical

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;  
$y = "100";

var_dump($x !== $y); // returns true because types are not equal
?>  

</body>
</html>



Output:

This will produce the following result



bool(true)


Example: > Greater than

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 90;
$y = 50;

var_dump($x > $y); // returns true because $x is greater than $y
?>  

</body>
</html>



Output:

This will produce the following result



bool(true)


Example: < Less than

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 20;
$y = 50;

var_dump($x < $y); // returns true because $x is less than $y
?>  

</body>
</html>



Output:

This will produce the following result



bool(true)


Example: >= Greater than or equal to

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 70;
$y = 70;

var_dump($x >= $y); // returns true because $x is greater than or equal to $y
?>  

</body>
</html>


Output:

This will produce the following result



bool(true)


Example: <= Less than or equal to

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 60;
$y = 60;

var_dump($x <= $y); // returns true because $x is less than or equal to $y
?>  

</body>
</html>



Output:

This will produce the following result



bool(true)


Example: <=> Spaceship

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;  
$y = 10;

echo ($x <=> $y); // returns -1 because $x is less than $y
echo "<br>";

$x = 10;  
$y = 10;

echo ($x <=> $y); // returns 0 because values are equal
echo "<br>";

$x = 15;  
$y = 10;

echo ($x <=> $y); // returns +1 because $x is greater than $y
?>  

</body>
</html>



Output:

This will produce the following result



-1
0
1