Home

Boolean Comparisons

Introduction

A Boolean comparison is an operation that compares two values to find out whether they are equal or one is higher than the other, using Boolean logic:

Different types of operators are available.

The Comparison for Equivalence

Equivalent ==: Two values are said to be equivalent if they can be evaluated as the same, usually after one has been cast or converted into another. To find out if two values are equivalent even if they are not the same type, use the equivalency operator represented as ==. The formula to follow is:

$value1 == $value2

If both values are equivalent, the comparison produces a true value. If the values cannot be assessed as being the same, the comparison produces false. As mentioned above, you can compare tweo Boolean values. For examples, some web controls, such as the check box, hold a Boolean value. You can find out whether the value of such a control is true or false. Here is an example:

employee1.php

<?php
echo "<form name='frmEmploymentDecision' action='employee2.php' method='post'>
      <h3>Human Resources: Employment Decision</h3>
      <table border='4'>
        <tr>
          <td>Employee Name:</td>
          <td>
	    <input name='txtEmployeeName' type='text' /></td>
        </tr>
        <tr>
          <td>Full-Time?</td>
          <td>
	    <input name='chkFullTime' type='checkbox'  />
            <input name='Submit' type='submit' value='Apply Benefits' /></td>
        </tr>
      </table>
    </div>
    </form>";
?>

employee2.php

<?php
echo "<form name='frmEmploymentDecision' method='post'>
      <h3>Human Resources: Employee Record</h3>";

      $benefits = "";
      $employeeName = htmlspecialchars($_POST['txtEmployeeName']);

      if($_POST["chkFullTime"] == true)
          $benefits = "Health, dental, vision, 401(K)";

echo "      <table border='4'>
        <tr>
          <td>Employee Name:</td>
          <td>
	    <input name='txtEmployeeName' type='text' value='";

echo $employeeName;

echo "' /></td>
        </tr>
        <tr>
          <td>Benefits:</td>
          <td>
	    <input name='txtBenefits' style='width: 250px' value='";

echo $benefits;

echo "' />
	     </td>
        </tr>
      </table>
    </div>
    </form>";
?>

This would produce:

The Comparison for Equivalence

The Comparison for Equivalence

The comparison can also be performed between strings. For example, you can get the value of a web control and compare it to a string of your choice. Here is an example:

<?php
echo "<form name='frmPayroll' method='post'>
      <div id='formulation'>
      <p id='main-title'>Employee Payroll</p>";

      $employeeName = $_POST['txtEmployeeName'];
      $hourlySalary = (float)htmlspecialchars($_POST['txtHourlySalary']);

      $hourlySalary = (float)htmlspecialchars($_POST['txtHourlySalary']);
      $weeklyTime   = (float)htmlspecialchars($_POST['txtWeeklyTime']);
      $weeklySalary = $hourlySalary * $weeklyTime;

      $incomeTax    = 99.10 + ($weeklySalary * 0.25);

      if($_POST['cbxMaritalStatus'] == 'Married')
          $incomeTax = 35.50 + ($weeklySalary * 0.15);

      $netPay = $weeklySalary  - $incomeTax;

echo "      <table border='4'>
        <tr>
          <td>Employee Name:</td>
          <td>
	    <input name='txtEmployeeName' type='text' value='";

echo $employeeName;

echo "' /></td>
        </tr>

        <tr>
          <td>Marital Status:</td>
          <td>
	    <select name='cbxMaritalStatus'>
              <option>Single</option>
              <option>Married</option>
            </select></td>
        </tr>
        <tr>
          <td>Hourly Salary:</td>
          <td>
	    <input name='txtHourlySalary' type='text' style='width: 60px' value='";

echo $hourlySalary;

echo "' /></td>
        </tr>
        <tr>
          <td>Weekly Time:</td>
          <td>
	    <input name='txtWeeklyTime' type='text' style='width: 60px' value='";

echo $weeklyTime;

echo "' />
	    <input name='Submit' type='submit' value='Calculate' />
          </td>
        </tr>

        <tr>
          <td>Income Tax:</td>
          <td>
	    <input name='txtIncomeTax' type='text' value='";

echo $incomeTax;

echo "' /></td>
        </tr>
        <tr>
          <td>Net Pay:</td>
          <td><input name='txtNetPay' type='text' value='";

echo $netPay;

echo"' /></td>
        </tr>
      </table>
    </div>
    </form>";
?>

This would produce:

The Comparison for Equivalence

In some cases, if the Boolean value of a variable or a web control is true, when performing the comparison, only the name of the variable or control is necessary instead of the whole equivalency expression. For example, instead of if($_POST["chkFullTime"] == true) ..., you can use:

employee2.php

<?php
echo "<form name='frmEmploymentDecision' method='post'>
      <h3>Human Resources: Employee Record</h3>";

      $benefits = "";
      $isFullTime = $_POST["chkFullTime"];
      $employeeName = htmlspecialchars($_POST['txtEmployeeName']);

      if($isFullTime)
          $benefits = "Health, dental, vision, 401(K)";

echo "      <table border='4'>
        <tr>
          <td>Employee Name:</td>
          <td>
	    <input name='txtEmployeeName' type='text' value='";

echo $employeeName;

echo "' /></td>
        </tr>
        <tr>
          <td>Benefits:</td>
          <td>
	    <input name='txtBenefits' style='width: 250px' value='";

echo $benefits;

echo "' />
	     </td>
        </tr>
      </table>
    </div>
    </form>";
?>

The Comparison for Equality

Equality ===: To compare two values of the same type for equality, use the equality operator represented as ===. The formula to follow is:

$value1 === $value2

The comparison for equivalency or equality is safely made on natural numbers (integers), characters, strings, dates, times, and objects. The comparison is risky and unpredictable on floating-point numbers (imagine comparing 0.001099 and 0.0011 for equality). Such a comparison should be avoided.

The Comparison for Non-Equivalence

Not Equivalent: <>: or != To find out if two values, whether they are the same type or not, are different, use the <> or the != operator. The formulas to follow are:

$value1 <> $value2
$value3 != $value4

If the values are different, the comparison produces a true result. If the values are equivalent based on their types, the comparison produces false.

The Comparison for Difference

Not Equal: !== To find out if two values of the same type are different, use the !== operator. The formula to follow is:

$value1 !== $value2

The Comparison for Lower Value

Less Than <: To find out if one value is lower than another, use the < operator. The comparison can be done as follows:

$value1 < $value2

If the value on the left of the operator is lower than the value on the right side, the operation produces a true value. Otherwise, the result is false.

The Comparison for Lower or Equal Value

Less Than Or Equal To <=: If one value is lower than or equal to another, to compare them, use the <= operator. This can be done as follows:

$value1 <= $value2

If the left value is lower than the value on the right side or both values are the same, the comparison produces a true result. If the value on the left is greater than the other and they are not equal, the comparison produces false.

The Comparison for Higher Value

Greater Than >: To find out if one value is greater than another value, compare them using an operator as >. It can be used as follows:

$value1 > $value2

If the value on the left of the operator is greater than the value on the right side, the operation produces a true value. Otherwise, it produces a false result.

The Comparison for Greater or Equal Value

Greater Than Or Equal To >=: If one value is greater than or equal to another, to compare them, use an operator as >=. It can be used as follows:

$value1 >= $value2

If the value on the left side of the operator is greater than the value on the right side or if both values are equal, the comparison produces a result as true. If the value on the left is greater than the value on the right and the values are also different, the comparison produces a result as false. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Gas Utility Company</title>
<style type="text/css">

#formulation
{
    margin: auto;
    width:  250pt;
}
#main-title
{
    font-size:   18pt;
    font-weight: bold;
    font-family: Georgia, "Times New Roman", Times, serif;
}
</style>
</head>
<body>

<?php
echo "<form name='frmCalculation' method='post'>
      <div id='formulation'>
      <p id='main-title'>Gas Utility Company</p>";

      $pricePerCCF    = 50.00;
      $consumption    = htmlspecialchars($_POST['txtConsumption']);
      
      if($consumption >= 0.50)
          $pricePerCCF = 35.00;

      $monthlyCharges = $consumption * $pricePerCCF;

echo "      <table border='4'>
        <tr>
          <td>Consumption:</td>
          <td>
	    <input name='txtConsumption' type='text' style='width: 80px' value='";

echo $consumption;

echo "' /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>
	    <input name='Submit' type='submit' value='Calculate' />
          </td>
        </tr>
        <tr>
          <td>Price per CCF:</td>
          <td><input name='txtPricePerCCF' type='text' value='";

echo $pricePerCCF;

echo"' /></td>
        </tr>
        <tr>
          <td>Monthly Charges:</td>
          <td><input name='txtMonthlyCharges' type='text' value='";

echo $monthlyCharges;

echo "' /></td>
        </tr>
      </table>
    </div>
    </form>";
?>
</body>
</html>

Here is an example of using the webpage:

If a Condition is True

If a Condition is True

The Spaceship Comparison

Spaceship: The comparison is performed on two natural numbers using the <=> operator as follows:

$value1 <=> $value2

The comparison produces a value as:

Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Employee Payroll</title>
<style type="text/css">

#formulation
{
    margin: auto;
    width:  250pt;
}
#main-title
{
    font-size:   18pt;
    font-weight: bold;
    font-family: Georgia, "Times New Roman", Times, serif;
}
</style>
</head>
<body>

<?php
echo "<form name='frmComparisons' method='post'>
      <div id='formulation'>
      <p id='main-title'>Comparisons</p>";

      $first  = htmlspecialchars($_POST['txtFirst']);
      $second = htmlspecialchars($_POST['txtSecond']);
      $result = ($first <=> $second);

echo "      <table border='4'>
        <tr>
          <td><input name='txtFirst' type='text' value='$first'></input></td>
          <td>&lt;=&gt;</td>
          <td><input name='txtSecond' type=checkbox value='$second'></input></td>
          <td><input name='Submit' type='submit' value='Produces'></input></td>
          <td><input name='txtResult' type='text' value='$result'></input></td>
        </tr>
      </table>
    </div>
    </form>";
?>
</body>
</html>

The Null Coalesce Comparison

Null coalesce: The comparison uses 3 operands and two operators as follows:

$value1 ?? $value2 ?? $value3

The operation produces:


Previous Copyright © 2015-2022, FunctionX Next