PHP Comparison Operators
Last updated onPHP, a widely used scripting language for web development, provides a plethora of features that contribute to its versatility as a tool for constructing dynamic and interactive websites. An essential element of programming in PHP revolves around the use of comparison operators. These operators empower developers to compare values and derive decisions based on the outcomes.
In this article, we will delve into PHP comparison operators, examining their types and exploring their effective applications in various scenarios.
Transitioning to the specific concept, it involves comparing two values to ascertain whether they share the same data type or differ in data types. The ultimate objective is to attain an accurate result, necessitating the consideration of an alternative execution path if required.
Below is a table that contains PHP comparison operators.
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 “Equal To” operator (==
), also known as the loose operator, utilizes double equals to compare two variables or values based solely on their values.
If we have two values that need to be compared for an exact match in value, we can use the equality operators. Here’s an example to illustrate this:
<?php
var_dump( 55 == 44 );
?>
The output would be like this: bool(false)
. The following involves the comparison 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 if we used the identical operator? Let’s explore that in the following section.
The 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 with 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, it was bool(true)
. That is because it is an identical operator, which checks for both value and data type.
In the previous example, $var_1 has a string data type, and $var_2 is an integer. However, both have the same value. So, it gave us “bool(false)”.
Anyway, in the next section, I will discuss the “Not Equality” operators, such as not equal and not identical. Let’s delve into that in-depth.
The Not Equal To Operator
The “Not Equal” operator is used to determine the boolean truth value if the two values are different in value. It takes two written forms, such as “<>” or “!=”.
<?php
var_dump( 55 != 44 ); // bool(true);
var_dump( 100 <> "100" ); // bool(false);
?>
So, the comparison was solely based on value. However, we will use the same example in the next operator. Let’s proceed with that.
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 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 both values and data types. This means we have a string data type that will not match with the integer value in the strict mode comparison.
Sometimes during development, you need to check if certain variables are greater than others. This is done to direct the code flow towards executing another set of instructions. Let’s explore 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 next example, we need to filter the age numbers that are greater than or equal to 18 years old from the PHP array. To achieve this, I will 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>";
This code should generate two arrays, one for youths 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 discuss 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 ” <= “.
Here, I will use the same example as in 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 the value of the first variable is greater than, less than, or equal to another value. It typically uses three operands — “<=>”. Let’s explore an example for each case.
$x is greater than $y, so it will return an integer value of “1”.
<?php
$x = 100;
$y = 50;
var_dump( $x <=> $y ); // int(1)
?>
$x is less than $y. So, the result would be an integer value of “-1”.
<?php
$x = 50;
$y = 100;
var_dump( $x <=> $y ); // int(-1)
?>
When you compare other types using this operator, it will yield the same results as in the above code. Let’s take a look at that.
Compare two arrays using the spaceship operator.
Note that the spaceship operator will compare arrays based on their 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 operator to compare two values in the PHP string data type. This comparison operates based on the ASCII values of the letters in alphabetical order. Let’s see how it works.
So, the example would be as follows.
<?php
var_dump( "b" <=> "c" ); // int(-1)
var_dump( "e" <=> "a" ); // int(1)
var_dump( "xe" <=> "xe" ); // int(0)
?>
In PHP float,, you can use the same approach, but here it depends on the float value, similar to the way it operates with integer numbers.
Take a look at the following example.
<?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 find more details in the PHP spaceship operator tutorial.
In the last two sections, I will explain two additional operators: ‘less than’ and ‘greater than’. Let’s move forward.
Using PHP Comparison within the Less Than Operator
This operator can be written as — “<“. It compares two values and produces a boolean value if the first value is less than the second one; otherwise, it yields a false result.
<?php
var_dump( 250 < 300 ); // bool(true);
var_dump( 300 < 250 ); // bool(false);
var_dump( 300 < 300); // bool(false);
?>
So, it will yield a true boolean value only if the first value is indeed less than the second one. Now, let’s explore the “greater than” operator.
Using PHP Comparison within the Greater Than Operator
This operator can be written as — “>”. It compares two values and generates a boolean value if the first value is greater than the second one; otherwise, it yields a false result.
<?php
var_dump( 250 > 300 ); // bool(false);
var_dump( 300 > 250 ); // bool(true);
var_dump( 300 > 300); // bool(false);
?>
Wrapping Up
In conclusion, PHP comparison operators play a pivotal role in web development, offering developers the tools to effectively compare values and make decisions based on the outcomes.
Throughout this exploration, we delved into various comparison operators, including the equality operator (==
), the identity operator (===
), the inequality operator (!=
), the not identical operator (!==
), and operators for greater than (>
), less than (<
), greater than or equal to (>=
), and less than or equal to (<=
). We also examined logical operators like AND (&&
), OR (||
), and NOT (!
).
The “spaceship” operator (<=>
) introduced a concise way to compare values and streamline decision-making. We explored its application in different data types, including strings and floats, understanding that it evaluates based on alphabetical order and numeric values.
By grasping these PHP comparison operators, developers can enhance the efficiency, accuracy, and readability of their code. Whether dealing with simple value comparisons or intricate conditions involving data types, mastering these operators is fundamental to creating robust and dynamic web applications. As we conclude this exploration, it is evident that a solid understanding of PHP comparison operators is a valuable asset for any developer seeking to build versatile and effective web solutions.
If you need more details about the article, read this article.