Home

Conditional Conjunctions & Disjunctions

Conditional Conjunctions

Nesting a Conditional Statement

A conditional statement has a body, which is from where the condition is defined to where its behavior ends. In the body of the conditional statement, you can create another conditional statement. This is referred to as nesting the condition. Here is an example:

payroll2a.php

<!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='frmPayroll2a' action='payroll2b.php' method='post'>
      <div id='formulation'>
      <p id='main-title'>Employee Payroll</p>

      <table border='4'>
        <tr>
          <td>Hourly Salary:</td>
          <td>
	    <input name='txtHourlySalary' type='text' 
	           style='width: 60px' value='0.00' /></td>
        </tr>
        <tr>
          <td>Overtime Paid:</td>
          <td>
	    <input type='checkbox' name='chkOvertimePaid'></input>
          </td>
        </tr>
        <tr>
          <td>Weekly Time:</td>
          <td>
	    <input name='txtWeeklyTime' type='text' 
	           style='width: 60px' value='0.00' />
	    <input name='Submit' type='submit' value='Calculate' />
          </td>
        </tr>
      </table>
    </div>
    </form>";
?>

</body>
</html>

payroll2b.php

<!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='frmPayroll2b' method='post'>
      <div id='formulation'>
      <p id='main-title'>Employee Payroll</p>";

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

      if($_POST["chkOvertimePaid"] == true) {
          if($weeklyTime > 40.00) {
              $overtime = $weeklyTime - 40.00;
              $overtimePay = $hourlySalary * 1.50 * $overtime;

              $weeklySalary = ($hourlySalary * 40.00) + $overtimePay;
          }

          $paidOvertime = 'Yes';
      }

echo "      <table border='4'>
        <tr>
          <td>Hourly Salary:</td>
          <td> $hourlySalary </td>
        </tr>
        <tr>
          <td>Overtime Paid:</td>
          <td> $paidOvertime </td>
        </tr>
        <tr>
          <td>Weekly Time:</td>
          <td> $weeklyTime </td>
        </tr>
        <tr>
          <td>Weekly Salary:</td>
          <td> $weeklySalary </td>
        </tr>
      </table>
    </div>
    </form>";
?>

</body>
</html>

Here is an example of using the webpage:

Nesting a Conditional Statement

Nesting a Conditional Statement

Nesting a Conditional Statement

Nesting a Conditional Statement

In the same way, you can nest one conditional statement in one, then nest that new one in another conditional statement, and so on.

Boolean Conjunctions

When you nest one condition in another condition as in:

if(condition1)	// The first or external condition
    if(Condition2)	// The second or internal condition
	statement(s)

you are in fact stating that "if condition1 verifies, AND THEN if condition2 verifies, do this...". The external condition must be verified first as being true or false (depending on how you wrote it). If that first (the external) condition is true, the second (the internal) condition is verified. If the first (the external) condition is not verified, the second (the external) condition is not verified.. To support  a simplified technique to apply this scenario, PHP provides the Boolean operators "and" and &&. Their primary formulas are:

condition1 and condition2
condition1 && condition2

You must formulate each condition to produce a true or a false result. The result is as follows:

  • If condition1 is true and condition2 is true, the whole expression produces a true result. Here are examples:
    <!DOCTYPE html>
    
    <html>
    
    <head>
    <title>Brokerage Company</title>
    <style type="text/css">
    #container
    {
    	width: 350px;
    	margin: auto;
    }
    #main-title
    {
    	font-size: 24pt;
    	font-family: Georgia, "Times New Roman", Times, serif
    }
    </style>
    </head>
    
    <body>
    <?php
        $commission = 0.00;
        $numberOfShares = (float)htmlspecialchars($_POST['txtNumberOfShares']);
        $pricePerShare  = (float)htmlspecialchars($_POST['txtPricePerShare']);
    
        $principal = $numberOfShares * $pricePerShare;
    
        if($principal >= 0.00 and $principal <= 2500.00)
            $commission = 26.25 + ($principal * 0.0014);
        elseif($principal > 2500.00 and $principal <= 6000.00)
            $commission = 45.00 + ($principal * 0.0054);
        elseif($principal > 6000.00 && $principal <= 20000.00)
            $commission = 60.00 + ($principal * 0.0028);
        elseif($principal > 20000.00 && $principal <= 50000.00)
            $commission = 75.00 + ($principal * 0.001875);
        elseif($principal > 50000.00 && $principal <= 500000.00)
            $commission = 131.25 + ($principal * 0.0009);
        else // if($principal > 500000.00)
            $commission = 206.25 + ($principal * 0.000075);
    
        $totalInvestment = $principal + $commission;
    
        echo "<div id='container'>
              <form method='post'>
              <p id='main-title'>Brokerage Company</p>
    
              <table style='width: 320px'>
                <tr>
                  <td>Number of Shares:</td>
                  <td><input name='txtNumberOfShares' type='text' 
                             style='width: 80px' value='$numberOfShares' /></td>
                </tr>
                <tr>
                  <td>Price per Share</td>
                  <td>
                    <input name='txtPricePerShare' type='text' 
                           style='width: 80px' value='$pricePerShare' />
                    <input name='btnCalculate' type='submit' value='Calculate' />
                  </td>
               </tr>
               <tr>
                 <td>Principal:</td>
                 <td><input name='txtPrincipal' type='text' value='$principal' /></td>
               </tr>
               <tr>
                 <td>Commission:</td>
                 <td><input name='txtCommission' type='text' value='$commission' /></td>
               </tr>
               <tr>
                 <td>Total Investment:</td>
                 <td><input name='txtTotalInvestment' 
                            type='text' value='$totalInvestment' /></td>
               </tr>
             </table>
             </form>
             </div>";
    ?>
    </body>
    </html>

    Here is an example of running the program:

    Boolean Conjunction

    Boolean Conjunction

  • If either Condition1 or Condition2 is false (including if both are false), the whole expression produces a false result

To make your code easier to read, it is a good idea to include each Boolean operation in its own parentheses. Here are examples:

. . .
<?php
    $commission = 0.00;
    $numberOfShares = (float)htmlspecialchars($_POST['txtNumberOfShares']);
    $pricePerShare  = (float)htmlspecialchars($_POST['txtPricePerShare']);

    $principal = $numberOfShares * $pricePerShare;

    if(($principal >= 0.00) and ($principal <= 2500.00))
        $commission = 26.25 + ($principal * 0.0014);
    elseif(($principal > 2500.00) and ($principal <= 6000.00))
        $commission = 45.00 + ($principal * 0.0054);
    elseif(($principal > 6000.00) && ($principal <= 20000.00))
        $commission = 60.00 + ($principal * 0.0028);
    elseif(($principal > 20000.00) && ($principal <= 50000.00))
        $commission = 75.00 + ($principal * 0.001875);
    elseif(($principal > 50000.00) && ($principal <= 500000.00))
        $commission = 131.25 + ($principal * 0.0009);
    else // if($principal > 500000.00)
        $commission = 206.25 + ($principal * 0.000075);
    . . .
?>
</body>
</html>

Depending on your program, if two conditions are not enough, you can create as many conjunctions as you want. The formula to follow is:

condition1 && condition2 && Condition3 && . . . && condition_n

When the expression is checked, if any of the operations is false, the whole operation is false. The only time the whole operation is true is if all of the operations are true.

Of course, you can nest a Boolean conjunction inside another conditional statement or another conjunction.

 
 
 

Boolean Disjunctions

Introduction

A Boolean disjunction is a conditional statement where you combine two or more conditions but only one of the conditions must be true for the whole expression to be true. This operation is performed using the Boolean "or" or || operators. The primary formulas to follow are:

condition1 or condition2

Or

condition1 || condition2

The operation works as follows:

  • If at least one of the individual conditions is true, the whole expression is true
  • If all operations are false, only then will the whole expression be false

As mentioned for the conjunction, it is a good idea to include each Boolean operation in its parentheses.

You can create a conditional statement that includes as many disjunctions as you want. The formula to follow is:

condition1 || condition2 || . . . || condition_n

The rule is the same: If any one of the individual operations is true, the whole expression is true. The whole expression is false only if all of the operations are false.

The Exclusive Disjunction

If you have two conditions where one of them must be true but the other must be false, you can use the XOR operator to perform the comparison. The operation is done using the xor operator. Its formula is:

condition1 xor condition2

With this condition:

  • If condition1 is true AND/BUT condition2 is false, the whole expression is true
  • If condition1 is false BUT condition2 is true, the whole expression is true
  • If both conditions are true or both conditions are false, the whole expression is false

Other Details on Conditional Statements

Negating a Conditional Statements

PHP provides an operator that considers the opposite of any Boolean expression. This is done with the exclamation point. The formula to follow is:

!expression

The expression can be the name of a Boolean variable or a formal logical expression. When the operation has been performed, the result is whatever is the opposite of the value.

Combining Conjunctions and Disjunctions

Conjunctions and disjunctions can be used in the same expression. A conjunction (or disjunction) can be used to evaluate one sub-expression while a disjunction (or conjunction) can be used to evaluate another sub-expression.

As seen previously, one way you can combine conditional statements is by nesting them.

 
   
 

Previous Copyright © 2015, FunctionX Next