Logical AND, OR and XOR
November 27th, 2009A condition tested in an if-statement can be composed of one or more conditions. This as known as a complex boolean expression where the expression evaluates to a boolean value of true or false. Keeping things simple, I will use two conditions (or two boolean expressions) in these examples.
Although I feel tempted to go to my previous posting and change all occurrences of condition to boolean expression, I’m not. Usually, I use the word “condition” or “check” to refer to a boolean expression tested in an if-statement.
Two boolean expressions are joined with a logical operator to make a complex boolean expression. Using pseudo code, this appears as follows:
if ((condition A) LOGICAL_OPERATOR (condition B)) then
// do this when true
end if
In 95% of everyday programming, you will encounter these logical operators:
- Logical AND
- Logical OR
On a rare occasion, you will encounter:
- Logical XOR
For an overkill effect, I am going to incorporate concepts from a course that I took a decade ago that I don’t use everyday: logical gates. What is important is how these gates behave.
Logic gates are a physical device that accepts electrical wire voltages as input. Personally, I never physically held nor wired a logic gate. My only experience has diagramming and running simulations on them.
Logical AND
The logical AND gate has two inputs and one output. The notation for an AND gate is.
The behavior of this gate is defined by a truth table. Truth tables, enumerate all possible input values and their corresponding output.
| A | B | A AND B |
| false | false | false |
| false | true | false |
| true | false | false |
| true | true | true |
Logical AND statements evaluate to true when all inputs are true. In PHP a logical AND is denoted by double ampersand symbol &&. It is also acceptable to use the word “and”; however, seeing it spelled out is a rarity.
<?php
$currentSpeed = 35;
$inConstructionZone = true;
$speedLimit = 30;
if (($currentSpeed > $speedLimit) && ($inConstructionZone === true)) {
echo("Condition is true; slowing down.\n");
$currentSpeed = $speedLimit;
}
echo("My current speed is $currentSpeed.\n");
?>
Applying the example above to a truth table, the A input is ($currentSpeed > $speedLimit) and B is ($inConstructionZone === true). A evaluates to (35 > 30) which is true. B is (true === true) which is also true. Since A is true and B is true, true AND true evaluates to true. The program executes the code-block contained within the if-statement.
Output:
Condition is true; slowing down.
My current speed is 30.
Here is another example.
<?php
$currentSpeed = 70;
$speedLimit = 65;
$isRaining = false;
if (($currentSpeed >= $speedLimit - 10) && ($isRaining === true)) {
echo("Condition is true; slowing down.\n");
$currentSpeed = $speedLimit - 10;
}
echo("My current speed is $currentSpeed.\n");
?>
Plugging in the two boolean expressions into the inputs of the logical AND truth table, A would be ($currentSpeed >= $speedLimit - 10) which is (70 >= 55), evaluating to true. B is ($isRaining === true) or (false === true), evaluating to false. AND requires both input to be true. B is false, and the expression evaluates to false. The code block in the if-statement is not executed in this scenario.
Output: My current speed is 70.
Logical OR
The logical OR gate accepts two input and has one output. The diagram for an OR gate is:
The OR gate’s truth table:
| A | B | A OR B |
| false | false | false |
| false | true | true |
| true | false | true |
| true | true | true |
A logical OR evaluates to true when at least one input is true. In PHP a logical OR is denoted by a double pipe symbol ||. It also acceptable to use the word “or,” but I seldom see logical operators spelled out.
<?php
$trafficLight = "RED";
$trafficLight = strtoupper(trim($trafficLight));
if (($trafficLight == "YELLOW") || ($trafficLight == "RED")) {
echo("Stop until the light turns green.\n");
}
else {
echo("Keep on truckin'\n");
}
?>
Substituting values for the variables, we have the boolean expression (“RED” == “YELLOW”) OR (“RED” == “RED”). This simplifies to false OR true. At least one input needs to be true, and one is. False OR true evaluates to true.
Output: Stop until the light turns green.
Here’s another example.
<?php
$amountDue = 99.95;
$couponCode = "";
$freeShippingCouponCode = "ABCEFGHI";
$hasFreeShipping = false;
if (($amountDue > 300.00) || ($couponCode == $freeShippingCouponCode)) {
$hasFreeShipping = true;
}
var_dump($hasFreeShipping);
?>
In the example above the condition tested is (($amountDue > 300.00) || ($couponCode == $freeShippingCouponCode)). A is ($amountDue > 300.00). After variable substitution this becomes (99.95 > 300.00) which evaluates to false. Our only hope now is for the right-side, B, of the complex expression to be true. B is ($couponCode == $freeShippingCouponCode). After variable substitution this is (“” == “ABCEFGHI”) which is false. Looking up in our truth false for OR behavior false OR false evaluates to false, and the code following the if-code block is not run.
Unfortunately writing echo("Has free shipping is $hasFreeShipping\n"); will not display the values of true and false to the screen. PHP displays nothing for false values and 1 for true values. So, I am writing var_dump($hasFreeShipping); as a crude measure of showing the contents that variable.
The output is: bool(false)
Logical XOR
The logical XOR gate accepts two inputs and one output. The diagram for a XOR gate is:
The corresponding truth table:
| A | B | A XOR B |
| false | false | false |
| false | true | true |
| true | false | true |
| true | true | false |
A logical XOR, or an eXclusive OR, evaluates to true when only one input is true. Should more than one input be true, an XOR evaluates to false. There is no shorthand notation for a logical XOR in PHP; it is spelled out as “xor”.
<?php
$isPlayer1Standing = false;
$isPlayer2Standing = true;
$isPlayer3Standing = false;
echo("Let's play musical chairs... and the music stops.\n");
if (($isPlayer1Standing === true) xor
($isPlayer2Standing === true) xor
($isPlayer3Standing === true)) {
echo("One person is standing; they are out of the game.\n");
}
else {
echo("Someone can sit down now.\n");
}
?>
Output:
Let's play musical chairs... and the music stops.
One person is standing; they are out of the game.


