·  PHP – Syntax – Operators – Comparison

 

- Info:

  - Comparison operators are used to compare values of two variables.

  - This way they provide a method to direct program flow depending on the autcome.

  - This tutorial is based on http://hr.php.net/ternary.

 

    

Type

Name

Description

 

 

$a == $b

Equal

TRUE if $a is

equal to $b after type juggling

 

$a != $b

Not equal

TRUE if $a is

not equal to $b after type juggling.

 

$a <> $b

Not equal

TRUE if $a is

not equal to $b after type juggling.

 

$a === $b

Identical

TRUE if $a is

of the same value and type as $b.

 

$a !== $b

Not identical

TRUE if $a is

not of the same value and type as $b.

 

$a < $b

Less than

TRUE if $a is

strictly less than $b.

 

$a > $b

Greater than

TRUE if $a is

strictly greater than $b.

 

$a <= $b

Less than or equal

TRUE if $a is

less than or equal to $b.

 

$a >= $b

Greater than or equal

TRUE if $a is

greater than or equal to $b.

 

- Additional Info:

  - PHP comparison operators should be used only for comparing numerical values.

    Strings should be compared using predefined PHP functions designed for that purpose.

 

- Example:

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

 

      $left  = 65;

      $right = 70;

 

      if ($left ==  $right) { print("Left    equals              right.\n"); }

      if ($left !=  $right) { print("Left is different from      right.\n"); }

      if ($left <>  $right) { print("Left is different from      right.\n"); }

 

      if ($left === $right) { print("Left is identical to        right.\n"); } #Same      value and type

      if ($left !== $right) { print("Left is not identical to    right.\n"); } #Different value or  type

 

      if ($left <   $right) { print("Left is smaller   then      right.\n"); }

      if ($left <=  $right) { print("Left is smaller   or equals right.\n"); }

      if ($left >   $right) { print("Left is greater   then      right.\n"); }

      if ($left >=  $right) { print("Left is greater   or equals right.\n"); }

 

    ?>