Comparison Operators : PHP



PHP Comparison Operators :

Assume variable A holds 10 and variable B holds 20 then:

Operator,Description,Example
==,Checks if the value of two operands are equal or not\, if yes then condition becomes true.,(A == B) is false.
!=,Checks if the value of two operands are equal or not\, if values are not equal then condition becomes true.,(A != B) is true.
>,Checks if the value of left operand is greater than the value of right operand\, if yes then condition becomes true.,(A > B) is false.
<,Checks if the value of left operand is less than the value of right operand\, if yes then condition becomes true.,(A < B) is true. >=,Checks if the value of left operand is greater than or equal to the value of right operand\, if yes then condition becomes true.,(A >= B) is false.
<=,Checks if the value of left operand is less than or equal to the value of right operand\, if yes then condition becomes true.,(A <= B) is true. [/table]

Example : Comparison Operators

<html>
<head><title>Comparison Operators</title><head>
<body>
<?php
    $a = 50;
    $b = 80;
    if( $a == $b ){
       echo "Test1 : a is equal to b<br/>";
    }else{
       echo "Test1 : a is not equal to b<br/>";
    }
    if( $a > $b ){
       echo "Test2 : a is greater than  b<br/>";
    }else{
       echo "Test2 : a is smaller than b<br/>";
    }
    if( $a < $b ){
       echo "Test3 : a is less than b<br/>";
    }else{
       echo "Test3 : a is greater than b<br/>";
    }
    if( $a != $b ){
       echo "Test4 : a is not equal to b<br/>";
    }else{
       echo "Test4 : a is equal to b<br/>";
    }
    if( $a >= $b ){
       echo "Test5 : a is grater than or equal to b<br/>";
    }else{
       echo "Test5 : a is smaller than b<br/>";
    }
    if( $a <= $b ){
       echo "Test6 : a is less than or equal to b<br/>";
    }else{
       echo "Test6 : a is greater than b<br/>";
    }
?>
</body>
</html>

Output :

Test1 : a is not equal to b
Test2 : a is smaller than b
Test3 : a is less than b
Test4 : a is not equal to b
Test5 : a is smaller than b
Test6 : a is less than or equal to b