In this tutorial, you will understand the PHP comparison operators such as strict operators, loose operators, not equal operators, identical operators, not identical operators, greater than operators and so many else.

This concept involves comparing two values to determine whether they have the same data type or different data types. The goal is to obtain an accurate result, which may require using an alternative execution path.

Below is a table containing PHP comparison operators.

OperatorsNameDescription
==Equal (Loose Operators )To compare two variables or values according to only values.
===Identical (Strict Operators)To compare two variables or values according to values and data type.
!= or <> Not EqualTo detect the truth if the two values are different according to only the value.
!==Not Identical To detect the truth if the two values are different according to only the value and data type.
>=Greater Than or Equal ToTo compare two values together, if one of them is greater than or equal to the other value, it will show the true value.
<=Less Than or Equal toTo compare two values together, if one of them is Less than or equal to the other value, it will show the true value.
<=>SpaceshipThese operators are collecting 3 meanings, such as
— Greater than
— Less than
— Equal to
And that to compare two values together and return integer data type
<Less ThanTo compare two values together, if one of them is Less than the other value, it will show the true value.
>Greater ThanTo compare two values together, if one of them is greater than the other value, it will show the true value.

That’s all, in the following sections we are going to discuss the PHP comparison operators in-depth. I will dive more into each one in detail.

The EQUAL TO Operator

The “equal to” operator (==), also known as the loose operator, uses double equals to compare two variables or values based solely on their values.

If we have two values that need to be compared for a match in value, we can use the equal operators. Here’s an example to illustrate this:

<?php 
  var_dump( 55 == 44 ); 
?>

The output would be like this: bool(false). The following is comparing of two matched values.

<?php 
  $var_1 = 5;
  $var_2 = 5;
  
  var_dump( $var_1 == $var_2 ); // bool(true)
?>

In the example, the output will be “true” as a boolean value, indicating that the two variables have the same value. However, if one variable contains a numerical value with a string data type and the other contains an integer value, the output will still be “true” as a boolean value.

<?php 
  $var_1 = "5";
  $var_2 = 5;
  
  var_dump( $var_1 == $var_2 ); // bool(true)
?>

But, what about if we used the identical operator? Let’s see that in the following section.

Identical Operator

The identical operators, also known as strict operators, are represented by triple equals (===) and are used to compare two variables or values based on both their value and data type.

So let’s continue the previous PHP code example.

Instead of the equal operator, we will replace it with a strict equal to see what is happening there.

<?php 
  $var_1 = "5";
  $var_2 = 5;
  
  var_dump( $var_1 === $var_2 );  
?>

The result is bool(false). But in the previous example, was bool(true). And that is because it is an identical operator which is checking for value and data type.

In the previous example, the $var_1 has a string data type and the $var_2 is an integer. But two both have the same value. So it gave us “bool(false)”.

Anyway, in the next operator, I will discuss more about the “Not equality” operators like not equal and not identical. Let’s see that in-depth.

The NOT EQUAL TO Operator

The “Not Equal” operator is to detect the truth boolean value if the two values are different in value. It takes two written shapes, such as — “<>” or — “!=”.

<?php 
  var_dump( 55 != 44 ); // bool(true);
  var_dump( 100 <> "100" ); // bool(false);
?>

So, the comparison was only in value. But we will use the same example in the next operator. Let’s do that.

Use the PHP Comparison within the NOT IDENTICAL Operator

The “not identical” operator checks for the true value if the two values are different according to only the value and data type.

I will use the same example.

<?php 
  var_dump( 100 !== "100" ); // bool(true);
?>

It produces a true value as a boolean data type because it checks for values and data types. That means we have a string data type that will not match with the integer value in the strict mode comparison.

Sometimes and during the development, you need to check if some variables are greater than any else. And that to direct the code flow to do another thing. Let’s see that in the next operator.

The GREATER THAN or EQUAL TO Operator

The “Greater Than” or “Equal To” to compare two values together, if one of them is greater than or equal to the other value, it will show the true value. Which is taking operators like this — “>=“.

<?php 
  var_dump( 55 >= 44 ); // bool(true); 
  var_dump( 55 >= 55 ); // bool(true);
?>

In the next example, we need to filter the age numbers that are greater than or equal to 18 years old from the PHP array. To do that, I will use the PHP for loop.

<?php 
  $all        = array( 35, 6, 4, 8, 12, 26, 9, 10, 11,18 , 15, 36, 30, 31, 32 );
  $children   = array();
  $youths     = array();
  
  // For loop
  for( $i =0; $i< count($all); $i++ ) {
    if( $all[$i] >= 18 ) {
      // Youths  
      $youths[] = $all[$i];
    } else {
      // Childrens
      $children[] = $all[$i];
    }
  }
  
  echo "<pre>";
  print_r( $youths );
  echo "</pre>";
  
  echo "<pre>";
  print_r( $children );
  echo "</pre>";
?>

This code should produce two arrays, one for youths and the other one for children.

Array
(
    [0] => 35
    [1] => 26
    [2] => 18
    [3] => 36
    [4] => 30
    [5] => 31
    [6] => 32
)

Array
(
    [0] => 6
    [1] => 4
    [2] => 8
    [3] => 12
    [4] => 9
    [5] => 10
    [6] => 11
    [7] => 15
)

In the following part, I am going to discuss the “less than” or “equal operator” with a small PHP example.

The LESS THAN or EQUAL TO Operator

The “less than” or “equal operator” To compare two values together, if one of them is Less than or equal to the other value, it will show the true value. This operator should be written like this — “<=“.

Here I will use the same example in the “greater than” operators.

<?php 
  var_dump( 55 <= 44 ); // bool(false); 
  var_dump( 55 <= 55 ); // bool(true);
?>

Let’s move to the “spaceship” operator.

The Spaceship Operator

The Spaceship Operator compares if the first variable value is greater than or less than or equal to another value. It usually takes these three operands — “<=>“. Let’s see an example for each case.

The $x is greater than the $y. So it will return an integer value — “1“.

<?php 
  $x = 100;
  $y = 50;
  var_dump( $x <=> $y ); // int(1)  
?>

The $x is less than the $y. So the result would be an integer value — “-1“.

<?php 
  $x = 50;
  $y = 100;
  var_dump( $x <=> $y ); // int(-1)  
?>

The $x is equal to the $y. So it will return an integer value — “0“.

<?php 
  $x = 100;
  $y = 100;
  var_dump( $x <=> $y ); // int(0)  
?>

When you compare the other types using this operator, it will show the same results as in the above code. Let’s see that.

Compare two arrays together using the spaceship operator.

Note that: the spaceship operator will compare the arrays in counts, values, and data types

<?php 
  
  // two arrays has the same values
  var_dump( array(1, 5, 6) <=> array( 1, 5, 6 ) ); // int(0)
  
  // two arrays the first is bigger than the other one
  var_dump( array(1, 5, 6) <=> array( 1, 6 ) ); // int(1)
  
  // two arrays the first is less than the other one
  var_dump( array( 5, 6) <=> array( 1, 6, 5 ) ); // int(-1)

?>

Using the spaceship in comparing the two values in the PHP string data type. This comparison is working according to the ASCII of the letter in alphabet order. Let’s see how it does work.

So the example would be like the following.

<?php  

  var_dump( "b" <=> "c" ); // int(-1)  
  
  var_dump( "e" <=> "a" ); // int(1)  

  var_dump( "xe" <=> "xe" ); // int(0)  
  
?>

In the PHP float, you can use the same, but here it depends on the float value like in the integer numbers.

Check the following examples.

<?php 
  var_dump( 21.5 <=> 21.4 ); // int(1); 
  var_dump( 21.5 <=> 21.6 ); // int(-1);
  var_dump( 21.5 <=> 21.5 ); // int(0);
?>

You can see more detail in the PHP spaceship operator tutorial.

In the last two sections, I will explain another two operators, “less than” and “greater than”. Let’s move forward.

Use the PHP Comparison within the LESS THAN Operator

This operator can be written as the — “<“. And that operator compares two values together and produces a boolean value if the first value is less than the last one, or else.

<?php 
  var_dump( 250 < 300 ); // bool(true);
  var_dump( 300 < 250 ); // bool(false); 
  var_dump( 300 < 300); // bool(false);   
?>

So, will have a true boolean value if only the first value is actually less than the second one. Let’s see the “greater than” operator.

Use the PHP Comparison within the GREATER THAN Operator

This operator can be written as the — “>“. To compare the two values together and generates a boolean value if the first value is greater than the last one, or else.

<?php 
  var_dump( 250 > 300 ); // bool(false);
  var_dump( 300 > 250 ); // bool(true); 
  var_dump( 300 > 300); // bool(false);   
?>

Wrapping Up

In this article, you understood what are the PHP comparison operators such as greater than, less than, equal to, and so many others.

If you need more details about the PHP data types, visit this article.