PHP Comparison Operators
Last updated onPHP 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.
Operators | Name | Description |
---|---|---|
== | 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 Equal | To determine the truth when the two values differ based solely on their values. |
!== | Not Identical | To determine the truth when the two values differ based on both the value and data type. |
>= | Greater Than or Equal To | To 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 to | To compare two values; if one of them is less than or equal to the other value, it will yield a true result. |
<=> | Spaceship | These 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 Than | To compare two values; if one of them is less than the other value, it will yield a true result. |
> | Greater Than | To 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
, then you will get x > y
1
:
<?php
$x = 100;
$y = 50;
var_dump( $x <=> $y ); // int(1)
?>
If
is less than $x
, it returns $y
-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
if so and true
otherwise.false
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
if the first value is actually less than the second.true
Use of Greater Than Operator in PHP
The greater-than operator checks if the first value is larger and returns
when that’s the case, but true
otherwise.false
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?
How do I check if two values are equal in PHP?
How can I compare both value and type in PHP?
What does the spaceship operator do in PHP?
How do I check if two values are not equal in PHP?
Can I compare strings in PHP using comparison operators?
How does the less than or equal to operator work in PHP?
What is the difference between `==` and `===` in PHP?
How can I use PHP comparison operators to check if a value is within a range?
What’s the best way to compare arrays in PHP?
How can I use PHP comparison operators to filter values in an array?