Specifications

try to use whichever conditional will be most readable in your situation. You will acquire a feel
for this with experience.
Iteration: Repeating Actions
One thing that computers have always been very good at is automating repetitive tasks. If there
is something that you need done the same way a number of times, you can use a loop to repeat
some parts of your program.
Bob wants a table displaying the freight cost that will be added to a customers order. With the
courier Bob uses, the cost of freight depends on the distance the parcel is being shipped. The
cost can be worked out with a simple formula.
We want our freight table to resemble the table in Figure 1.7.
PHP Crash Course
C
HAPTER 1
1
PHP CRASH
COURSE
43
FIGURE 1.7
This table shows the cost of freight as distance increases.
Listing 1.3 shows the HTML that displays this table. You can see that it is long and repetitive.
LISTING 1.3 freight.html—HTML for Bob’s Freight Table
<html>
<body>
<table border = 0 cellpadding = 3>
<tr>
<td bgcolor = “#CCCCCC” align = center>Distance</td>
<td bgcolor = “#CCCCCC” align = center>Cost</td>
</tr>
<tr>
<td align = right>50</td>
<td align = right>5</td>
03 7842 CH01 3/6/01 3:39 PM Page 43