PHP Comparison Operators

Last updated on

PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values are equal, different, greater, or less than one another. From number and string comparisons to more complex data, these operators work to provide flow control for your code.

In this article, we will go over the main comparison operators in PHP, from basic equality to type-sensitive checks, so that you can make decisions in your code with precision and confidence.

Comparison operators in PHP are shown in the table below.

OperatorsNameDescription
==Equal (Loose Operators )To compare two variables or values based solely on their values.
===Identical (Strict Operators)To compare two variables or values based on both values and data type.
!= or <>Not EqualTo determine the truth when the two values differ based solely on their values.
!==Not IdenticalTo determine the truth when the two values differ based on both the value and data type.
>=Greater Than or Equal ToTo compare two values; if one of them is greater than or equal to the other value, it will yield a true result.
<=Less Than or Equal toTo compare two values; if one of them is less than or equal to the other value, it will yield a true result.
<=>SpaceshipThese operators encompass three meanings, including:— Greater than— Less than— Equal toAnd that is to compare two values together and return an integer data type.
<Less ThanTo compare two values; if one of them is less than the other value, it will yield a true result.
>Greater ThanTo compare two values; if one of them is greater than the other value, it will yield a true result.

That concludes the overview; in the following sections, we will delve into the PHP comparison operators in-depth. Will understand the detailed explanations for each one.

The Equal To Operator

The operator for "Equal To" is also referred to as the loose operator and makes use of double equals to check if the values of two variables are equal. This comparison observes the value only and does not show concern for data type.

This is the operator to use when you need to determine whether two values are equal:

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

The result here would be bool(false) because the values do not match. Now, here is an example of comparing two values that do match, which would therefore give a bool(true) output instead.

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

In this example, the output will be true as a boolean; it shows that both variables hold the same value. Even when one variable is holding a number as a string, such as "5", and another as an integer, such as 5, the output will be true because the == operator compares only the values, not the types.

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

But what about when using the identical operator? We will look at that in the following section.

The Identical Operator

The identifiers of Identity, aka strict operators, are triple equals (===) and are used in the comparison of two variables or values regarding their value and data type.

So, let’s continue with the previous PHP code example.

Instead, we are going to use a strict equal in place of the equal operator just to see what's going on there.

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

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

In the previous example, the data type of $var_1 is a string and $var_2 is an integer. But the same value holds for both. So, it gave us "bool(false)".

Anyway, in the following, I will be discussing the "Not Equality" operators, namely: not equal (not identical). Let's elaborate on that.

The Not Equal To Operator

The "Not Equal" operator checks if two values are different. It returns true if they do not match. PHP provides two ways to write this: either <> or !=.

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

This comparison differs only in value, not in type. Let's proceed with the same example to see how it works in the next operator.

Using PHP Comparison within the Not Identical Operator

The "not identical" operator checks for the true value if the two values are different based on both 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 both values and data types. This means we have a string data type that will not match with the integer value in strict mode comparison.

Sometimes, in development, you want to know if certain variables are more important than others. This does that to steer the code flow toward executing another set of instructions. Let's learn that in the next operator.

The Greater Than or Equal To Operator

The "Greater Than" or "Equal To" operator is used to compare two values. If one of them is greater than or equal to the other value, it will yield a true result. This operator is represented as ">=".

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

In the following example, we will filter out the age numbers that are greater than or equal to 18 years old from the PHP array. For this purpose, I am going to use a 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>";

It should create two lists: one for youth and the other 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 section, I am going to explain the “Less Than” or “Equal To” operator with a small PHP example.

The Less Than or Equal To Operator

The “less than or equal to” operator is used to compare two values. If one of them is less than or equal to the other value, it will yield a true result. This operator should be written as "<=".

Now here I will use the same example that was used for the "greater than" operators.

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

Let’s move on to the “spaceship” operator.

The Spaceship Operator

The spaceship operator (<=>) compares whether one value is greater than, less than, or equal to another. It returns an integer depending on the comparison: 1 if the first value is greater, -1 if it’s less, and 0 if they're equal. Let's look at a few examples.

  If x > y, then you will get 1:  

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

If $x is less than $y, it returns -1:

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

This operator works the same way for other data types. For example, it compares arrays based on count 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)

?>

For strings, it compares their values in ASCII order:

<?php  

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

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

It works like integers but uses decimal points with floats:

<?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);
?>

For more details, check the PHP spaceship operator tutorial. Next, we’ll explore the less than and greater than operators.

Using the Less Than Operator in PHP

The less-than operator checks whether one value is smaller than another. It returns true if so and false otherwise.

For example:

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

This operator only returns true if the first value is actually less than the second.

Use of Greater Than Operator in PHP

The greater-than operator checks if the first value is larger and returns true when that’s the case, but false otherwise.

Here is an example:

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

Wrapping Up

PHP comparison operators are very useful in coding; with these, you can compare values and make decisions. We have been dealing with quite a few of them, each having its own definitions, such as equality (==), identity (===), inequality (!=), not identical (!==), less than (<), greater than (>), and others like <= and >=. There is also the spaceship operator, which you can use for easy comparison of values between different types of data.

The spaceship operator will sort in either numeric or alphabetical order. Mastering these operators can save you countless headaches when planning and writing clear, swift code—whether comparing two values or doing something more complex. Gaining a good grasp of comparison operators in PHP can be one of the biggest keys to creating flexible, responsive applications.

Frequently Asked Questions (FAQs)

  • What are PHP comparison operators?

    PHP comparison operators let you compare two values and determine if they are equal, different, greater, or less than each other. You’ll use operators like <=>, ==, !=, <, >, for basic comparisons, and others like === and !== for strict comparisons that consider both value and data type.
  • How do I check if two values are equal in PHP?

    To check if two values are equal in PHP, use the == operator. This will return true if the values match and false if they don’t, ignoring data type differences. Example:
    var_dump(55 == 44); // bool(false)
    var_dump(5 == "5"); // bool(true)
    
  • How can I compare both value and type in PHP?

    Use the === operator to compare both the value and data type. This is called a strict comparison and only returns true if both match exactly. Example:
    var_dump(5 === "5"); // bool(false)
    var_dump(5 === 5); // bool(true)
    
  • What does the spaceship operator do in PHP?

    The spaceship operator <=> returns 1 if the first value is greater, -1 if it’s less, and 0 if the values are equal. It’s a quick way to check three possible outcomes in a single step. Example:
    var_dump(100 <=> 50); // int(1)
    var_dump(50 <=> 100); // int(-1)
    var_dump(50 <=> 50); // int(0)
    
  • How do I check if two values are not equal in PHP?

    The != or <> operators check if two values differ. They return true if the values are not equal and false if they are. Example:
    var_dump(55 != 44); // bool(true)
    var_dump(100 <> "100"); // bool(false)
    
  • Can I compare strings in PHP using comparison operators?

    Yes, PHP allows you to compare strings using operators like ==, !=, <, and >. The comparison considers ASCII values, so alphabetical order matters. Example:
    var_dump("a" < "b"); // bool(true)
    var_dump("e" > "a"); // bool(true)
    
  • How does the less than or equal to operator work in PHP?

    The less than or equal to operator <= returns true if the first value is less than or equal to the second value. Otherwise, it returns false. Example:
    var_dump(55 <= 44); // bool(false)
    var_dump(55 <= 55); // bool(true)
    
  • What is the difference between `==` and `===` in PHP?

    == checks if two values are equal, ignoring data types. === checks both value and data type, so it only returns true if both match exactly. Example:
    var_dump(5 == "5"); // bool(true)
    var_dump(5 === "5"); // bool(false)
    
  • How can I use PHP comparison operators to check if a value is within a range?

    You can use the >= and <= operators together to check if a value is within a specified range. Example:
    $age = 18;
    var_dump($age >= 13 && $age <= 19); // bool(true)
    
  • What’s the best way to compare arrays in PHP?

    The spaceship operator <=> is a handy way to compare arrays in PHP. It will compare arrays based on length, values, and data types. Example:
    var_dump(array(1, 5, 6) <=> array(1, 5, 6)); // int(0)
    var_dump(array(1, 5, 6) <=> array(1, 6)); // int(1)
    var_dump(array(5, 6) <=> array(1, 6, 5)); // int(-1)
    
  • How can I use PHP comparison operators to filter values in an array?

    You can use comparison operators inside loops to filter out specific values from an array. For example, use >= to separate numbers above a certain value. Example:
    $numbers = array(35, 6, 4, 8, 12, 26, 9, 10, 11, 18, 15);
    $adults = array();
    foreach ($numbers as $number) {
        if ($number >= 18) {
            $adults[] = $number;
        }
    }
    print_r($adults);
    
Share on: