Loops and PHP Decision Making - Breaking Out of a Loop
(Page 5 of 5 )
PHP provides the equivalent of an emergency stop button for a loop: the break statement. Normally, the only way out of a loop is to satisfy the expression that determines when to stop the loop. If the code in the loop finds an error that makes continuing the loop pointless or impossible, you can break out of the loop by using the break statement. It's like getting your shoelace stuck in an escalator. It really doesn't make any sense for the escalator to keep going! But those old-fashioned ones did!
Possible problems you might encounter in a loop include running out of space when you're writing to a file or attempting to divide by zero. In Example 4-16, we simulate what can happen if you divide based on an unknown entry initialized from a form submission (that could be a user-supplied value). If your user is malicious or just plain careless, she might enter a negative value where you're expecting a positive value (although this should be caught in your form validation process). In the code that's executed as part of the loop, the code checks to make sure the $counter is not equal to zero. If it is, the code calls break
Looping
Example 4-16. Using break to avoid division by zero
<?php
$counter = -3;
for (; $counter < 10; $counter++){
// Check for division by zero
if ($counter == 0){
echo "Stopping to avoid division by zero.";
break;
}
echo "100/$counter<br />";
}
?>
This displays the following:
100/-3
100/-2
100/-1
Stopping to avoid division by zero.
Of course, there may be times when you don't want to just skip one execution of the loop code. The continue statement performs this for you.
continue Statements
You can use the continue statement to stop processing the current block of code in a loop and jump to the next iteration of the loop. It's different from break in that it doesn't stop processing the loop entirely. You're basically skipping ahead to the next iteration. Make sure you are modifying your test variable before the continue statement, or an infinite loop is possible. Example 4-17 shows the preceding exampleusing continue instead of break.
Example 4-17. Using continue instead of break
<?php
$counter=-3;
for (;$counter<10;$counter++){
//check for division by zero
if ($counter==0){
echo "Skipping to avoid division by zero.<br>";
continue;
}
echo "100/$counter ",100/$counter,"<br />";
}
?>
The new output is as follows:
100/-3 -33.3333333333
100/-2 -50
100/-1 -100
Skipping to avoid division by zero.
100/1 100
100/2 50
100/3 33.3333333333
100/4 25
100/5 20
100/6 16.6666666667
100/7 14.2857142857
100/8 12.5
100/9 11.1111111111
Notice that the loop skipped over the $counter value of zero but continued with the next value.
We've now covered all of the major program flow language constructs. We've discussed the building blocks for controlling program flow in your programs. Expressions can be as simple as TRUE or FALSE or as complex as relational comparison with logical operators. The expressions combined with program flow control constructs like the if statement and switch make decision-making easy.
We also discussed while, do ... while, and for loops. Loops are very useful for common dynamic web page tasks such as displaying the results from a query in an HTML table.
Note: this chapter in the full book ended with a question-and-answer section. Answers are included in the book's index. Why not buy the book and check it out yourself?
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.
|
|