Core PHP

PHP Lesson 5: Boolean Expressions

Overview

This lesson presents the basis for programmatic decision making via boolean expressions. A boolean expression is an expression made up of boolean values that are joined together by logical operators. Those boolean values may be generated by comparisons that performed via comparison operators. Putting comparisons together with logical operators allows us to generate a rich set of operations for programmatic decision making.

Comparison Operators

Comparison operators compare two data values and determine their relationship via a boolean value. These comparisons include the basic less than, greater than, and equal to operators, which can joined to create the operators for less than or equal to, greater than or equal to, and the not equal to operators. Additionally, PHP provides a second equality operator, called identically equal to, which compares the type as well as the value. These comparisons can be used to generate boolean values that can be assembled into boolean expressions.

This program first generates a series of checks on the two integer values and reports the result as a boolean value; recall that boolean values are displayed as 1 for true and nothing for false when they are converted to string values. The second set of comparisons applies equality operators to the equivalent float and int value to illustrate how the identically equaly === operator differs from the ordinary equality operator. Note that the not equal operator != is equivalent to the prior not equal operator <>. Finally, the last comparison tests two integer values that are equivalent to show that the identically equal operator reports true in this case.

Prog1.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$iNine = 9;
$iTen = 10;
$fTen = 10.0;

echo "<pre>";
echo 'Common Operators<br />';
echo '($iNine < $iTen) -> '.($iNine < $iTen).'<br />';
echo '($iNine > $iTen) -> '.($iNine > $iTen).'<br />';
echo '($iNine <= $iTen) -> '.($iNine <= $iTen).'<br />';
echo '($iNine >= $iTen) -> '.($iNine >= $iTen).'<br />';
echo '($iNine == $iTen) -> '.($iNine == $iTen).'<br />';
echo '($iNine <> $iTen) -> '.($iNine <> $iTen).'<br /><br />';

echo 'Equality Checks<br />';
echo '($iTen == $fTen) -> '.($iTen == $fTen).'<br />';
echo '($iTen === $fTen) -> '.($iTen === $fTen).'<br />';
echo '($iTen != $fTen) -> '.($iTen != $fTen).'<br />';
echo '($iTen !== $fTen) -> '.($iTen !== $fTen).'<br /><br />';

$iTEN = 10;
echo '($iTen === $iTEN) -> '.($iTen === $iTEN).'<br />';
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog1.php

 

Logical Operators

There are four logical operators available in PHP: And, Or, Xor, and Not. The operators And, Or, and, Xor are binary operators that act on two boolean values, while the Not operator is unary since it operates on one value. There are two ways to write both the And and Or operators: either And is equivalent to && and Or is equivalent to ||. If you know logic, these operators should be familiar. If not, I will clarify them in the next section.

Prog2.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$bTrue = true;
$bFalse = false;

echo "<pre>";
echo 'Binary Operators<br />';
echo '($bTrue && $bFalse) -> '.($bTrue && $bFalse).'<br />';
echo '($bTrue || $bFalse) -> '.($bTrue || $bFalse).'<br />';
echo '($bTrue AND $bFalse) -> '.($bTrue AND $bFalse).'<br />';
echo '($bTrue OR $bFalse) -> '.($bTrue OR $bFalse).'<br />';
echo '($bTrue XOR $bFalse) -> '.($bTrue XOR $bFalse).'<br /><br />';

echo 'Not Operator<br />';
echo '!$bTrue -> '.!$bTrue.'<br />';
echo '!$bFalse -> '.!$bFalse.'<br /><br />';
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog2.php

 

Truth Tables

To demonstrate how the logical operators act, this program prints out truth tables for each of the operations. The three binary operators have four possible combinations, while the unary operator has only two. Notice that if the first value of the And operator is false, the result is false. Likewise, if the first value of the Or operator is true, the result is true. This allows PHP to optimize the results for these operators and avoid evaluating the second operand in these cases.

Prog3.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$bTrue = true;
$bFalse = false;

echo "<pre>";
echo 'AND Truth Table<br />';
echo '($bTrue AND $bTrue) -> '.($bTrue AND $bTrue).'<br />';
echo '($bTrue AND $bFalse) -> '.($bTrue AND $bFalse).'<br />';
echo '($bFalse AND $bTrue) -> '.($bFalse AND $bTrue).'<br />';
echo '($bFalse AND $bFalse) -> '.($bFalse AND $bFalse).'<br /><br />';

echo 'OR Truth Table<br />';
echo '($bTrue OR $bTrue) -> '.($bTrue OR $bTrue).'<br />';
echo '($bTrue OR $bFalse) -> '.($bTrue OR $bFalse).'<br />';
echo '($bFalse OR $bTrue) -> '.($bFalse OR $bTrue).'<br />';
echo '($bFalse OR $bFalse) -> '.($bFalse OR $bFalse).'<br /><br />';

echo 'XOR Truth Table<br />';
echo '($bTrue XOR $bTrue) -> '.($bTrue XOR $bTrue).'<br />';
echo '($bTrue XOR $bFalse) -> '.($bTrue XOR $bFalse).'<br />';
echo '($bFalse XOR $bTrue) -> '.($bFalse XOR $bTrue).'<br />';
echo '($bFalse XOR $bFalse) -> '.($bFalse XOR $bFalse).'<br /><br />';

echo 'NOT Truth Table<br />';
echo '!$bTrue -> '.!$bTrue.'<br />';
echo '!$bFalse -> '.!$bFalse.'<br /><br />';
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog3.php

 

Examples of Boolean Expressions

To demonstrate how boolean expressions can be used in practice, I wrote this program that performs two checks on whether a value is contained in an interval and one check to evaluate an absolute value expression. Logical operators allow us to assemble multiple expressions into more complex ones to create sophisticated decision making in programs.

Prog4.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$fX = 2.49;
$fY = 5.29;
$fZ = 9.71;

echo "<pre>";
echo 'Open Interval Tests<br />';
echo "Check if \$fY = $fY &isin; ($fX, $fZ) = (\$fX, \$fZ)<br />";
echo '(($fX < $fY) AND ($fY < $fZ)) -> '."(($fX < $fY) AND ($fY < $fZ)) -> "
  .(($fX < $fY) AND ($fY < $fZ)).'<br />';
echo "Check if \$fX = $fX &isin; ($fY, $fZ) = (\$fY, \$fZ)<br />";
echo '(($fY < $fX) AND ($fX < $fZ)) -> '."(($fY < $fX) AND ($fX < $fZ)) -> "
  .(($fY < $fX) AND ($fX < $fZ)).'<br /><br />';

echo 'Absolute Value Test<br />';
echo "Check if |\$fY - $fZ| > $fX<br />";
echo '(($fY > $fZ + $fX) OR ($fY < $fZ - $fX)) -> '
  ."(($fY > $fZ + $fX) OR ($fY < $fZ - $fX)) -> "
  .(($fY > $fZ + $fX) OR ($fY < $fZ - $fX)).'<br />';
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog4.php

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.