Control Structures
-
while
do while
for
foreach
break
continue
while
while loops are the simplest type of loop in PHP. They behave just
like their C counterparts. The basic form of a while statement is:
while (expr)
statement
While loops tells PHP to execute
the nested statement(s) repeatedly, as long as the while expression
evaluates to TRUE.
The value of the expression is checked each time
at the beginning of the loop, so even if this value changes during
the execution of the nested statement(s), execution will not stop
until the end of the iteration (each time PHP runs the statements
in the loop is one iteration). Sometimes, if the while expression
evaluates to FALSE from the very beginning, the nested statement(s)
won't even be run once.
Like with the if statement, you can group multiple statements within
the same while loop by surrounding a group of statements with curly
braces, or by using the alternate syntax:
while (expr):
statement
...
endwhile;
do-while
do-while loops are very similar to while loops, except the truth
expression is checked at the end of each iteration instead of in
the beginning.
In do-while, the first iteration of a do-while loop is guaranteed
to run (the truth expression is only checked at the end of the iteration).
There is just one syntax for do-while loops:
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
for
for loops are the most complex loops in PHP. They behave like their
C counterparts. The syntax of a for loop is:
for (expr1; expr2; expr3)
statement
The first expression (expr1) is evaluated (executed) once unconditionally
at the beginning of the loop.
In the beginning of each iteration, expr2 is evaluated. If it evaluates
to TRUE, the loop continues and the nested statement(s) are executed.
If it evaluates to FALSE, the execution of the loop ends.
At the end of each iteration, expr3 is evaluated (executed).
Each of the expressions can be empty. expr2 being empty means the
loop should be run indefinitely. This may not be as useless as you
might think, since often you'd want to end the loop using a conditional
break statement instead of using the for truth expression.
PHP also supports the alternate "colon syntax" for for
loops.
for (expr1; expr2; expr3):
statement
...
endfor;.
foreach
foreach simply gives an easy way to iterate over
arrays. foreach works only on arrays, and will issue an error when
you try to use it on a variable with a different data type or an
uninitialized variable. There are two syntaxes; the second is a
minor but useful extension of the first:
The first form loops over the array given by array_expression. On each loop, the value of the current
element is assigned to $value and the internal
array pointer is advanced by one (so on the next loop, you'll be
looking at the next element).
The second form does the same thing, except that the current element's
key will be assigned to the variable $key on
each loop.
break
break ends execution of the current for, foreach, while, do-while or switch
structure.
break accepts an optional numeric argument which tells it how many
nested enclosing structures are to be broken out of.
<?php
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(, $val) = each($arr)) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}
/* Using the optional argument. */
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>
continue
continue is used within looping structures to
skip the rest of the current loop iteration and continue execution
at the condition evaluation and then the beginning of the next
iteration.
Note: Note that in PHP the switch statement is considered a looping
structure for the purposes of continue.
continue accepts an optional numeric argument which tells it how
many levels of enclosing loops it should skip to the end of.
<?php
while (list($key, $value) = each($arr)) {
if (!($key % 2)) { // skip odd members
continue;
}
do_something_odd($value);
}
$i = 0;
while ($i++ < 5) {
echo "Outer<br />\n";
while (1) {
echo " Middle<br />\n";
while (1) {
echo " Inner<br />\n";
continue 3;
}
echo "This never gets output.<br />\n";
}
echo "Neither does this.<br />\n";
}
?>
|