·  PHP – Syntax – Operators – Logical

 

- Info:

  - Logical operators are used to combine boolean values, those that can be only TRUE or FALSE.

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

  - This tutorial is based on http://hr.php.net/manual/en/language.operators.logical.php.

 

    

Type

Name

Description

 

$a &&  $b

AND

True if both $a and $b are true

 

$a AND $b

AND

True if both $a and $b are true

 

$a ||  $b

OR

True if either $a or $b is true

 

$a OR  $b

OR

True if either $a or $b is true

 

$a XOR $b

Exclusive OR

True if only $a or only $b is true

 

      !$b

NOT

True if $b is not true

 

   NOT $b

NOT

True if $b is not true

 

- Example:

  - Create PHP Console Application Test.php.

 

    Test.php

    <?php

 

      #Test variables.-------------------------------------------------------------------

      $left  = 65;

      $right = 70;       

       

      #Logical operators.----------------------------------------------------------------

      if (           !   ($left==80)) { print("$left is  NOT equal to 80 \n"); }

      if ( $left> 50 &&  $right==70 ) { print("$left >50 AND $right==70  \n"); }

      if ( $left!=50 ||  $right> 90 ) { print("$left!=50 OR  $right> 90  \n"); }

      if ( $left> 50 and $right==70 ) { print("$left >50 AND $right==70  \n"); }

      if ( $left!=50 xor $right> 90 ) { print("$left!=50 XOR $right> 90  \n"); }

      if ( $left!=50 or  $right> 90 ) { print("$left!=50 OR  $right> 90  \n"); }

 

    ?>