Specifications

do..while Loops
The final loop type we will mention behaves slightly differently. The general structure of a
do..while statement is
do
expression;
while( condition );
A do..while loop differs from a while loop because the condition is tested at the end. This
means that in a do..while loop, the statement or block within the loop is always executed at
least once.
Even if we take this example in which the condition will be
false at the start and can never
become true, the loop will be executed once before checking the condition and ending.
$num = 100;
do
{
echo $num.”<BR>”;
}
while ($num < 1 );
Breaking Out of a Control Structure or Script
If you want to stop executing a piece of code, there are three approaches, depending on the
effect you are trying to achieve.
If you want to stop executing a loop, you can use the break statement as previously discussed
in the section on switch. If you use the break statement in a loop, execution of the script will
continue at the next line of the script after the loop.
If you want to jump to the next loop iteration, you can instead use the
continue statement.
If you want to finish executing the entire PHP script, you can use exit. This is typically useful
when performing error checking. For example, we could modify our earlier example as fol-
lows:
if( $totalqty == 0)
{
echo “You did not order anything on the previous page!<br>”;
exit;
}
The call to exit stops PHP from executing the remainder of the script.
PHP Crash Course
C
HAPTER 1
1
PHP CRASH
COURSE
47
03 7842 CH01 3/6/01 3:39 PM Page 47