PHP Comparison Operators
Operators In PHP
Comparison Operators
In this tutorial we discus the different comparison operator and the use of these comparison operator. Comparison operator are used for compare two values and the result of comparison operator is always True or False. Comparison operator used in transfer of control statement.
Following Table contain the comparison operator with example:
|
Operator |
Example |
Function of Comparison Operator |
|
= = |
$variable1= = $variable2 |
if both variable has Same value then return True other wise False |
|
!= |
$variable1!= $variable2 |
if both variable has Not Same value then return True other wise False |
|
< |
$variable1< $variable2 |
if variable1 is Less Then variable2 then return True other wise False |
|
> |
$variable1> $variable2 |
if variable1 is Grater Then variable2 then return True other wise False |
|
<= |
$variable1<= $variable2 |
if variable1 is Less Then or Equal To variable2 then return True other wise False |
|
>= |
$variable1 >= $variable2 |
if variable1 is Grater Then or Equal To variable2 then return True other wise False |
Now we describe the each operator and its function:
== ( Has the same value as ) The double equals sign show the relation between two variable are same. If the relation between two vraible or realy same then its return True Value other wise its return False value.
$variable1=10;
$variable2=10
if ($variable1 == $variable2) {
}
The above statement return True.
!= (Is NOT the same value as) . This comparison operator test the one value is NOT the same as another.
$variable1=10;
$variable2=10
if ($variable1 != $variable2) {
}
The above statement return False.
You can also text the two value are Greater then or Less then each other for this purpose LESS THEN (<) and GREATER THEN (>) operator are used.
$variable1=10;
$variable2=20
if ($variable1>variable2)
echo $variable1 . "is large" ;
if ($variable1< variable2)
echo $variable2 . "is large" ;
In above statement first comparison return False and second comparison return True . So The Second statement execute and Display the result " 20 is large " .