If you haven’t used them before, logical operators may at first seem a little daunting.
Operator Description Example && And $j == 3 && $k == 2 and Low-precedence and $j == 3 and $k == 2 || Or $j < 5 || $j > 10 or Low-precedence or $j < 5 or $j > 10 ! Not ! ($j == $k) xor Exclusive or $j xor $k
Note that && is usually interchangeable with and; the same is true for || and or. However, because and and or have a lower precedence you should avoid using them except when they are the only option, as in the following statement, which must use the or operator (|| cannot be used to force a second statement to execute if the first fails):
$html = file_get_contents($site) or die(“Cannot download from $site”);
The most unusual of these operators is xor, which stands for exclusive or and returns a TRUE value if either value is TRUE, but a FALSE value if both inputs are TRUE or both inputs are FALSE. To understand this, imagine that you want to concoct your own cleaner for household items. Ammonia makes a good cleaner, and so does bleach, so you want your cleaner to have one of these. But the cleaner must not have both,
because the combination is hazardous. In PHP, you could represent this as follows:
$ingredient = $ammonia xor $bleach;
In this example, if either $ammonia or $bleach is TRUE, $ingredient will also be set to TRUE. But if both are TRUE or both are FALSE, $ingredient will be set to FALSE.