Comparison Operators in PHP

Comparison operators are those operators in PHP which are used to compare the values of two operands or two quantities.

For example, you can use these operators to know which is a bigger number, comparing the age of two persons, comparing the price of two items, etc.

Since comparison operators determine the relationship between them by comparing operands, these operators are also called relational operators in PHP.

These operators return a Boolean value (either true or false) based on the comparison. We often use these operators within conditional statements, such as if, while, and for loops to control the flow of the program.

Types of Comparison Operators in PHP


PHP provides several comparison operators to perform a specific comparison. They are as:

  • Equal (==)
  • Identical (===)
  • Not equal (!=)
  • Not identical (!==)
  • Less than (<)
  • Greater than (>)
  • Less than or equal to (<=)
  • Greater than or equal to (>=)

Let’s understand each operator with the help of examples.

Learn all types of comparison operators in PHP.

Equal Operator

The equal operator (==) in PHP is used to check whether the values of two variables are equal, regardless of their data type. If the values are equal, the operator returns true; otherwise, it returns false.

Example 1:

<?php
$a = 5; // Integer
$b = "5"; // String

if ($a == $b) {
   echo "The values are equal.";
} else {
   echo "The values are not equal.";
}
?>
Output:
      The values are equal.

In this example, we have compared the string value “5” to an integer value 5. Since both values are equal after the conversion, the result will be true, and the output will be “The values are equal.”

Always remember that PHP automatically converts the data types of the values being compared so that they can be compared properly. This process is called type juggling.


For example, if you compare an integer and a string with the equal (==) operator, PHP will convert the string to a number and then compare the two values. Even though the data types are different, whether one is a number and the other is text, PHP tries to find a common type to compare between them.

Identical Operator

The identical operator (===) is similar to the equal operator, but also checks the data types of variables. This operator will return true if values and data types of variables are identical. Otherwise, it returns false value. Unlike the equal operator (==), it does not perform type juggling.

Example 2:

<?php
$a = 10; // Integer
$b = "10"; // String

if ($a === $b) {
   echo "Both values are identical.";
} else {
   echo "The values are not identical.";
}
?>
Output:
       The values are not identical.

In this example, the comparison fails because one is an integer, and the other is a string value, even though their values are numerically the same.

Not equal Operator

The not equal operator (!=) is used to check whether two values or variables on the left and right of the operator are not equal. If the values are not equal, it returns true; otherwise, it returns false.


Example 4:

<?php
$a = 10;
$b = 20;
var_dump($a != $b);
?>
Output:
      bool(true)

In this example, the result is true because 10 is not equal to 20.

Not Identical Operator

The not identical operator (!==) is similar to the not equal operator, but it also takes into account data types of the variables. This operator will return true if the values and/or data types are not equal.

Example 5:

<?php
$a = 20;
$b = "20";
var_dump($a !== $b);
?>
Output:
      bool(true)

As you can see, the result is true because 20 is an integer value which is not identical to the string value 20.

Less Than Operator

The less than operator (<) checks if the value of the variable on the left side of the operator is less than the value on the right side. If the left side value is smaller, it returns true. Otherwise, it returns false.

Example 5:

<?php
$a = 10;
$b = 30;
if ($a < $b) {
    echo "$a is less than $b.";
}
?>
Output:
       10 is less than 30.

Greater Than Operator

The greater than operator (>) checks if the value of the variable on the left side of the operator is greater than the value on the right side. If the left side value is larger, it returns true. Otherwise, it returns false.

Example 6:

<?php
$a = 30;
$b = 20;
if ($a > $b) {
    echo "$a is greater than $b.";
}
?>
Output:
      30 is greater than 20.

Less Than or Equal To Operator

The less than or equal to operator (<=) checks if the value of the variable on the left side of the operator is less than or equal to the value on the right side. If the left side value is less than or equal to, it returns true. Otherwise, it returns false.

Example 7:

<?php
$a = 80;
$b = 80;
var_dump($a <= $b);
?>
Output:
       bool(true)

In this example, the result obtained is true because the value 80 of the variable $a is equal to the value of variable $b.

Greater Than or Equal To Operator

The greater than or equal to operator (>=) checks if the value of the variable on the left side of the operator is greater than or equal to the value on the right side. If the left side value is greater than or equal to, it returns true. Otherwise, it returns false.

Example 8:

<?php
$a = 90;
$b = 80;
var_dump($a >= $b);
?>
Output:
      bool(true)

Checking Age for Voting Eligibility

Let’s take an example in which we will check if a person is eligible to vote based on their age using the greater than or equal to (>=) operator.

Example 9:

<?php
$age = 18;
if ($age >= 18) {
   echo "You are eligible to vote.";
} else {
   echo "You are not eligible to vote.";
}
?>
Output:
       You are eligible to vote.

Best Practices for Using Comparison Operators in PHP


There are two key points that you should remember while using comparison operators in PHP. They are as:

  • Always use the identical operator (===) instead of not equal (==) when comparing values because it checks both value and data type. This prevents unexpected results due to type juggling.
  • Use the not identical operator (!==) for strict comparisons because it ensures that both the value and data type are different.