PHP Comparison Operators With Examples

Comparison Operators in PHP allow us to compare different values. The values can be both integer or string. There are 10 different types of operators in PHP. PHP Comparison Operators may seem to be very simple, however, there is a very minute difference between a few of them. It is very necessary to understand the differences in order to avoid bugs in your code. In this article, I will discuss all the comparison operators and highlight the differences between them.

PHP Comparison Operators

Let’s have a look at the table giving a brief about all the comparison operators in PHP.

NameOperatorExampleDescription
Equal==$x == $yTRUE if $x is equal to $y
Identical===$x === $yTRUE if $x is equal to $y and both are of the same data type
Not Equal!=$x != $yTRUE if $x is not equal to $y
Not Equal<>$x <> $yTRUE if $x is not equal to $y
Not Identical!==$x !== $yTRUE if $x is not equal to $y, or they are not of the same data type.
Less Than<$x < $yTRUE if $x is strictly less than $y.
Greater Than>$x > $yTRUE if $x is strictly more than $y.
Less Than or Equal To<=$x <= $yTRUE if $x is less than or equal to $y.
More Than or Equal To>=$x >= $yTRUE if $x is more than or equal to $y.
Spaceship<=>$x <=> $yAn integer less than, equal to, or greater than zero when $x is respectively less than, equal to, or greater than $y. Available as of PHP 7.

Equal To Operator (==)

The PHP Equal To Operator checks if the two values are equal or not.

<?php
	var_dump(1 == 1); //true
	var_dump(0 == 1); //false
	var_dump('a' == 'a'); //true
	var_dump('concatly' == 'concatly'); //true
	var_dump(true == true); //true
?>

However, the equal to operator does not validate the data type of the two variables. As a result, integer 1 is equal to string 1. This can be further explained with the help of the following example:

<?php
	var_dump(1 == '1'); //true
?>

Note: If you compare a string with an integer value, then each string is first converted into an integer and the comparison is performed numerically. This type conversion does not take place in Identical (===) operators.

<?php
	var_dump(0 == "a"); // 0 == 0 -> true
	var_dump("1" == "01"); // 1 == 1 -> true
	var_dump("10" == "1e1"); // 10 == 10 -> true
	var_dump(100 == "1e2"); // 100 == 100 -> true
?>

Warning: Never use equality check for floats. Use the Epsilon method instead. For more information, read about floats.

<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;

if(abs($a-$b) < $epsilon) {
    echo "true";
}
?>

Identical Operator (===)

The Identical Operator works the same way as the Equal To Operator. However, there is one major difference between the two. The Equal To operator does not take into account the data type of both operands, while the latter does. Therefore, an integer 1 is equal to string 1, but they are not identical.

<?php
	var_dump(1 === 1); //true
	var_dump(1 === '1'); //false
?>

Unlike Equal To Operator, the identical operator does not perform type conversion on comparing a string and an integer.

<?php
	var_dump('a' === 0); // false
	var_dump('a' == 0); //true
?>

Not Equal To (!= And <>)

The Not Equal To operator works the exact opposite of the Equal To Operator (==). Operands are not checked for data types.

Note: The two symbols (!= and <>) can be used interchangeably.

<?php
	var_dump(1 != 1); //false
	var_dump(0 <> 1); //true
	var_dump('a' != 'a'); //false
	var_dump('concatly' <> 'concatly'); //false
	var_dump(true != true); //false
	var_dump(1 <> '1'); //false
?>

Not Identical (!==)

The Not Identical Operator works the opposite of Identical Operator. Also, it takes care of data types of the operands. For instance, consider the following examples:

<?php
	var_dump(1 !== 1); //false
	var_dump(1 !== '1'); //true

	var_dump('a' !== 0); // true
	var_dump('a' != 0); //false
?>

Less Than Operator (<)

The Less Than operator returns TRUE if the value on the left-hand side is smaller than the right-hand side.

<?php
	var_dump(1 < 1); //false
	var_dump(0 < 1); //true
	var_dump(1.0 < 1); //false
	var_dump(1.1 < 1.2); //true
	var_dump(100 < TRUE); // false - same as (bool)100 < TRUE
?>

Greater Than Operator (>)

The Greater Than operator works the exact opposite way of the less than operator.

<?php
	var_dump(1 > 1); //false
	var_dump(0 > 1); //false
	var_dump(1.0 > 1); //false
	var_dump(1.1 > 1.2); //false
	var_dump(100 > TRUE); // false - same as (bool)100 < TRUE
?>

Less Than And Equal To Operator (<=)

As the name suggests, less than & equal to operator works the same way as the less than operator. Moreover, it also checks for equality of the two operands.

<?php
        var_dump(1 < 1); //false
        var_dump(1 <= 1); //true
?>

Greater Than And Equal To Operator (>=)

The Greater Than & Equal to Operator checks if one operand is greater than or equal to the other.

<?php
        var_dump(1 > 1); //false
        var_dump(1 >= 1); //true
?>

Spaceship Operator (<=>)

Syntax: $x <=> $y

The spaceship operator results in an integer less than, equal to, or greater than zero when $x is respectively less than, equal to, or greater than $y. It is only available as of PHP 7. For instance, consider the following examples:

<?php  
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
 
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
 
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
 
echo "a" <=> "aa"; // -1
echo "zz" <=> "aa"; // 1
 
// Arrays
echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1
 
// Objects
$a = (object) ["a" => "b"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 0
 
$a = (object) ["a" => "b"]; 
$b = (object) ["a" => "c"]; 
echo $a <=> $b; // -1
 
$a = (object) ["a" => "c"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 1
 
// not only values are compared; keys must match
$a = (object) ["a" => "b"]; 
$b = (object) ["b" => "b"]; 
echo $a <=> $b; // 1

?>

Courtesy: The above example is taken from the official PHP documentation of Spaceship Operator.

In simpler words, the spaceship operator works like the below function:

<?php
	function spaceshipOperator($x, $y) {
		if ($x == $y) { //if $x==$y then 0
			return 0;
		} else if ($x < $y) { //if $x < $y then -1
			return -1;
		} else { //if $x > $y then 1
			return 1;
		}
	}
?>

Other Comparison Operators

There are two more PHP Comparison Operators that we will be discussing in this post.

Ternary Operator ( ?: )

The ternary operator assigns default values to an expression.

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

<?php
	
	$pageNumber = (!empty($_GET['page']) ? $_GET['page'] : 1);

	//The above statement is identical to
	if (!empty($_GET['page'])) {
		$page = $_GET['page'];
	}  else {
		$page = 1;
	}

?>

Null Coalescing Operator ( ?? )

The Null Coalescing Operator is available only after PHP 7. Consider the following example:

<?php
// Example usage for: Null Coalesce Operator
$page = $_GET['page'] ?? 1;

// The above is identical to this if/else statement
if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page = 1;
}
?>

The expression ($x) ?? ($y) results in $y if $x is null, and $x otherwise. It does not emit a notice if the left-hand expression is NULL just like PHP isset() Function. It is very useful in validating array keys.

Conclusion

In this article, we discussed Comparison Operators in PHP. We also distinguished between the operators with examples. You can read other articles on PHP on Concatly. Also, you may go through the official PHP documentation on comparison operators.

Spread the Knowledge
Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *