Page 73 - Open Soource Technologies 304.indd
P. 73
Unit 3: Flow-Control Statements and PHP in Web Page
conditional variable before beginning the next iteration, so it will just keep processing the same Notes
error over and over again.
// bad code - potential infinite loop
$x = 0;
$y = 0;
$rVal[0] = “”;
do {
if (isset($aName[$x])) {
if (is_int($aName[$x])) {
$rVal[$y] = “Bad Value at $aName[$x]”;
$y++;
continue;
}
$bName[$x] = someProc($aName[$x]);
}
$x++;
}
When using break to get out of a condition, be aware that it ignores if
statements. This is so you can use an if statement to test whether you need to
terminate the current conditional.
3.4.3 Stepping Out Multiple Levels
PHP also gives you the option to step out of multiple levels when working with nested conditional
iterative statements. Once again, the if statement is not included since it is used to test whether
to make such a step.
Both the break and the continue statements can be followed by a number specifying how many
levels of nesting to step out. Best way to explain this is to create a little programmatic map:
statements;
// continue 4 jumps to here
while (loop 1 condition) {
statements;
// continue 3 jumps to here
while (loop 2 condition) {
statements;
// continue 2 jumps to here
while (loop3 condition) {
statements;
// continue or continue 1 jumps to here
while (loop 4 condition) {
LOVELY PROFESSIONAL UNIVERSITY 67