Specifications
echo “<font color=red>”;
echo “You did not order anything on the previous page!<br>”;
echo “</font>”;
}
The three lines of code enclosed in curly braces are now a block of code. When the condition
is true, all three lines will be executed. When the condition is false, all three lines will be
ignored.
A Side Note: Indenting Your Code
As already mentioned, PHP does not care how you lay out your code. You should indent your
code for readability purposes. Indenting is generally used to enable us to see at a glance which
lines will only be executed if conditions are met, which statements are grouped into blocks,
and which statements are part of loops or functions. You can see in the previous examples that
the statement which depends on the
if statement and the statements which make up the block
are indented.
else Statements
You will often want to decide not only if you want an action performed, but also which of a set
of possible actions you want performed.
An else statement allows you to define an alternative action to be taken when the condition in
an if statement is false. We want to warn Bob’s customers when they do not order anything.
On the other hand, if they do make an order, instead of a warning, we want to show them what
they ordered.
If we rearrange our code and add an else statement, we can display either a warning or a sum-
mary.
if( $totalqty == 0 )
{
echo “You did not order anything on the previous page!<br>”;
}
else
{
echo $tireqty.” tires<br>”;
echo $oilqty.” bottles of oil<br>”;
echo $sparkqty.” spark plugs<br>”;
}
We can build more complicated logical processes by nesting if statements within each other.
In the following code, not only will the summary only be displayed if the condition
$totalqty
== 0 is true, but also each line in the summary will only be displayed if its own condition
is met.
PHP Crash Course
C
HAPTER 1
1
PHP CRASH
COURSE
39
03 7842 CH01 3/6/01 3:39 PM Page 39