Specifications

echo $num.”<BR>”;
$num++;
}
At the beginning of each iteration, the condition is tested. If the condition is false, the block
will not be executed and the loop will end. The next statement after the loop will then be exe-
cuted.
We can use a while loop to do something more useful, such as display the repetitive freight
table in Figure 1.7.
Listing 1.4 uses a while loop to generate the freight table.
LISTING 1.4 freight.php—Generating Bob’s Freight Table with PHP
<body>
<table border = 0 cellpadding = 3>
<tr>
<td bgcolor = “#CCCCCC” align = center>Distance</td>
<td bgcolor = “#CCCCCC” align = center>Cost</td>
</tr>
<?
$distance = 50;
while ($distance <= 250 )
{
echo “<tr>\n <td align = right>$distance</td>\n”;
echo “ <td align = right>”. $distance / 10 .”</td>\n</tr>\n”;
$distance += 50;
}
?>
</table>
</body>
</html>
for Loops
The way that we used the while loops previously is very common. We set a counter to begin
with. Before each iteration, we tested the counter in a condition. At the end of each iteration,
we modified the counter.
We can write this style of loop in a more compact form using a for loop.
PHP Crash Course
C
HAPTER 1
1
PHP CRASH
COURSE
45
03 7842 CH01 3/6/01 3:39 PM Page 45