Last updated on November 27th, 2023
What are comparison operators in PHP?
Comparison operators in PHP are used to compare values and determine if a particular condition is true or false in the PHP code. They are essential for decision-making and controlling the flow of the programs.
What types of comparison operators are there?
There are 7 main types of comparison operators:
1. Equal Operator (==)
Checks if two values are equal in content, regardless of their data types.
2. Not Equal Operator (!=)
Checks if two values are not equal in content, regardless of their data types.
3. Greater Than Operator (>)
Checks if the value on the left is greater than the value on the right.
4. Less Than Operator (<)
Checks if the value on the left is less than the value on the right.
5. Greater Than or Equal To Operator (>=)
Checks if the value on the left is greater than or equal to the value on the right.
6. Less Than or Equal To Operator (<=)
Checks if the value on the left is less than or equal to the value on the right.
7. Strict Equality Operator (===)
The === operator in PHP is known as the “Identical” or “Strict Equality” operator.
The === operator checks both the value and data type.
If both value and data type match, the expression is true.
If either the value or data type doesn’t match, the expression is false.
It’s useful when you want to ensure not only equality but also the same data type.
In contrast, the == operator checks only the value, but not the data type.
Examples of using the comparison operators
1. Equal (==) Operator:
<?php $number1 = 5; $number2 = 5; if ($number1 == $number2) { echo "The numbers are equal."; } else { echo "The numbers are not equal."; } ?>
Result: The script will display “The numbers are equal.”
In this script, the == operator checks if $number1 is equal to $number2. Since both variables have the value 5, the condition is true, and the message “The numbers are equal.” is displayed.
2. Not Equal (!=) Operator:
<?php $value1 = "apple"; $value2 = "banana"; if ($value1 != $value2) { echo "The values are not the same."; } else { echo "The values are the same."; } ?>
Result: The script will display “The values are not the same.”
Here, the != operator checks if $value1 is not equal to $value2. Since the values are different (“apple” and “banana”), the condition is true, and it displays “The values are not the same.”
3. Greater Than (>) Operator:
<?php $score1 = 85; $score2 = 92; if ($score1 > $score2) { echo "Player 1 has a higher score."; } else { echo "Player 2 has a higher score."; } ?>
Result: The script will display “Player 2 has a higher score.”
In this script, the > operator checks if $score1 is greater than $score2. Since $score2 (92) is greater than $score1 (85), the condition is not true, and it displays “Player 2 has a higher score.”
4. Less Than (<) Operator:
<?php $age1 = 25; $age2 = 30; if ($age1 < $age2) { echo "Person 1 is younger."; } else { echo "Person 2 is younger."; } ?>
Result: The script will display “Person 1 is younger.”
Here, the < operator checks if $age1 is less than $age2. Since $age1 (25) is less than $age2 (30), the condition is true, and it displays “Person 1 is younger.”
5. Greater Than or Equal To (>=) Operator:
<?php $quantity1 = 10; $quantity2 = 10; if ($quantity1 >= $quantity2) { echo "Quantity 1 is greater than or equal to Quantity 2."; } else { echo "Quantity 1 is less than Quantity 2."; } ?>
Result: The script will display “Quantity 1 is greater than or equal to Quantity 2.”
In this script, the >= operator checks if $quantity1 is greater than or equal to $quantity2. Since both quantities are equal (both are 10), the condition is true, and it displays “Quantity 1 is greater than or equal to Quantity 2.”
6. Less Than or Equal To (<=) Operator:
<?php $price1 = 25; $price2 = 30; if ($price1 <= $price2) { echo "Price 1 is less than or equal to Price 2."; } else { echo "Price 1 is greater than Price 2."; } ?>
Result: The script will display “Price 1 is less than or equal to Price 2.”
Here, the <= operator checks if $price1 is less than or equal to $price2. Since $price1 (25) is less than $price2 (30), the condition is true, and it displays “Price 1 is less than or equal to Price 2.”
7. Strict Equality (===) Operator:
<?php $number = 5; $text = "5"; if ($number === $text) { echo "They are identical."; } else { echo "They are not identical."; } ?>
Result: The script will display “They are not identical.”
In this example, we have a variable $number with a value of 5 (integer – because it is a number) and a variable $text with a value of “5” (string – because it is enclosed in quotation marks). When we use the === operator to compare them, it checks both the value and the data type.
Since $number is an integer and $text is a string, even though their values are the same (both are 5), they are not considered identical because their data types are different. Therefore, the condition is false, and it displays “They are not identical.”