php logical operators

Much like the arithmetic operators, logical operators will probably play a major role in many of your PHP applications, providing a way to make decisions based on the values of multiple variables. Logical operators make it possible to direct the flow of a program and are used frequently with control structures, such as the if conditional and the while and for loops.

Logical operators are also commonly used to provide details about the outcome of other operations, particularly those that return a value.


<?php
file_exists("filename.txt") OR echo "File does not exist!";
?>

One of two outcomes will occur:

  • The file filename.txt exists
  • sentence "File does not exist!" will be output
Example Label Outcome
$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 || $b OR True if either $a or $b is true
$a OR $b OR True if either $a or $b is true
!$a NOT True if $a is not true
NOT $a NOT True if $a is not true
$a XOR $b Exclusive OR True if only $a or only $b is true




Content