Updated March 23, 2023
Introduction to Comparison Operators in PHP
The Word Comparison in Comparison Operators in PHP itself says that the operators are generally used for comparing any two values/variable values (variable values can be a string or a number or any other to compare). Equal, Identical, Not Equal, Not identical, Greater than, Less than, Greater than or equal to, Less than or equal to are some of the comparison operator names to compare any two types of similar type of values based on our requirement.
Types of Comparison Operators in PHP
There are different types of comparison operators in PHP programming language too just like the other programming languages. Check out about each comparison operators below with the illustrated examples.
1. Equal Comparison Operator ( == )
Equal Operator result will be TRUE only if the 1st variable value is equal to the 2nd variable value. If the 1st variable’s value is not equal to the 2nd variable’s value then FALSE will be the result/output of the comparison.
Example
This is the program to compare two values (strings or numbers) which are assigned to the variables as values. If those values are the same TRUE will be the output else FALSE will be the output. Based on that output the remaining code will run.
Code:
<?php
//1. comparing only numerical values/numbers using two variables
$pavan = 2;
$kumar = 5;
if($pavan==$kumar){
echo "TRUE : Because the two variable's values are same \n";
}
else{
echo "FALSE : Because the two variable's values are not same \n";
}
//2. Program to compare two string values
$a = "pavan";
$b = "pavan";
if($a==$b){
echo "TRUE : String values assigned to the two variables are same \n";
}
else{
echo "FALSE : String values assigned to the two variables are not same \n";
}
?>
Output:
2. Identical Comparison Operator (===)
This identical operator will give results as TRUE if the two variables values belong to the same data type variables else the result will be FALSE.
Example
The below program will be bool(false) because the two values which are in x1, y1 variables don’t belong to same data type so the result will be false.
Code:
<?php
$x1 = 100;
$y1 = "100";
var_dump($x1 === $y1); // will give result as false because types are not at all equal
?>
Output:
3. Not Equal Comparison Operator (!= or <>)
Not the Equal Operator result will become TRUE if the 1st variable’s value is not the same as the 2nd variable’s value else the result will be FALSE. Check the examples below and let know by yourself.
Example #1
Code:
<?php
$pavan1 = 1;
$sake1 = 2;
if($pavan1!=$sake1){
echo "TRUE :: variables values are not same as you expected";
}
else{
echo "FALSE :: variables values are same as not you expected";
}
?>
Output:
Example #2
Code:
<?php
$pavan1 = 1;
$sake1 = 2;
if($pavan1<>$sake1){
echo "TRUE :: variables values are not same as you expected .";
}
else{
echo "FALSE :: variables values are same as not you expected";
}
?>
Output:
4. Not Identical Comparison Operator (!==)
Not Identical operators will produce the TRUE result only when the values of the two variables do not belong to the same data types else the Not identical operator will produce the FALSE result if the data types of the variable’s value are the same.
Example
This program below is to illustrate how the not identical comparison operator operates.
Code:
<?php
$x2 = 100;
$y2 = "100";
var_dump($x2 !== $y2); // returns/provide result as true because types are not at all equal
?>
Output:
5. Less than Comparison Operator (<)
Less than the operator is used to check whether the 1st variable value is less than the 2nd variable value or 2nd variable value is less than the 1st variable value.
Example
The below program will provide result/statements which is in the IF condition because the x3 is less than y3 which is in the IF condition.
Code:
<?php
$x3 = 1473;
$y3 = 1474;
if($x3<$y3){
echo "x3 value :: $x3 \n";
echo "y3 value :: $y3 \n";
echo "x3 value is less than y3 value \n";
}
else{
echo "x3 value is less than y3 value";
}
?>
Output:
6. Greater than Comparison Operator (>)
Greater than operator is used to check whether the 1st variable value is greater than the 2nd variables value or 2nd variables value is greater than the 1st variable’s value. These comparison operators are very helpful when performing some operations in many programs from simple to complex.
Example
The below greater than operator’s program is to implement and to check which variables value is greater than the other variable value.
Code:
<?php
$x4 = 2020;
$y4 = 2019;
echo "x4 value :: $x4 \n";
echo "y4 value :: $y4 \n";
if($x4>$y4){
echo "x4 value is greater than y4 value \n";
}
else{
echo "y4 value is less than x4 value";
}
?>
Output:
7. Less than or Equal to Comparison Operator (<=)
Less than or Equal to the operator will helps in checking whether the 1st variable value is less than or equal to the 2nd variable value or not. It will check and prolong its program to proceed further.
Example
Code:
<?php
$x5 = 2020;
$y5 = 2020;
echo "x5 value :: $x5 \n";
echo "y5 value :: $y5 \n";
if($x5<=$y5){
echo "TRUE :: x5 value is less than or equal to y5 value \n";
}
else{
echo "FALSE :: y5 value is less than x5 value";
}
?>
Output:
8. Greater than or Equal to Comparison Operator (>=)
Greater than or Equal to operator helps in checking which number/variable’s value is greater than or equal to which number/other variables value. It also requires two variables values.
Example
X6 variables value can either be greater than or equal to the y6 variable’s value. Even though x6,y6 variables value are the same it will execute the statements in the IF condition only.
Code:
<?php
$x6 = 2020;
$y6 = 2020;
echo "x6 value :: $x6 \n";
echo "y6 value :: $y6 \n";
if($x6>=$y6){
echo "TRUE :: x6 value is greater than or equal to y6 value \n";
}
else{
echo "FALSE :: y6 value is less than x6 value";
}
?>
Output:
Recommended Articles
This is a guide to Comparison Operators in PHP. Here we discuss the Introduction and the types of comparison operators along with its different examples and code implementation. You may also have a look at the following articles to learn more –