Specifications
if( $totalqty == 0)
{
echo “You did not order anything on the previous page!<br>”;
}
else
{
if ( $tireqty>0 )
echo $tireqty.” tires<br>”;
if ( $oilqty>0 )
echo $oilqty.” bottles of oil<br>”;
if ( $sparkqty>0 )
echo $sparkqty.” spark plugs<br>”;
}
elseif Statements
For many of the decisions we make, there are more than two options. We can create a sequence
of many options using the elseif statement. The elseif statement is a combination of an
else and an if statement. By providing a sequence of conditions, the program can check each
until it finds one that is true.
Bob provides a discount for large orders of tires. The discount scheme works like this:
• Less than 10 tires purchased—no discount
• 10-49 tires purchased—5% discount
• 50-99 tires purchased—10% discount
• 100 or more tires purchased—15% discount
We can create code to calculate the discount using conditions and if and elseif statements.
We need to use the AND operator (&&) to combine two conditions into one.
if( $tireqty < 10 )
$discount = 0;
elseif( $tireqty >= 10 && $tireqty <= 49 )
$discount = 5;
elseif( $tireqty >= 50 && $tireqty <= 99 )
$discount = 10;
elseif( $tireqty > 100 )
$discount = 15;
Note that you are free to type elseif or else if—with and without a space are both correct.
If you are going to write a cascading set of elseif statements, you should be aware that only
one of the blocks or statements will be executed. It did not matter in this example because all
the conditions were mutually exclusive—only one can be true at a time. If we wrote our condi-
tions in a way that more than one could be true at the same time, only the block or statement
following the first true condition would be executed.
Using PHP
P
ART I
40
03 7842 CH01 3/6/01 3:39 PM Page 40