break statement usage
| From: | Erik Price | Date: | Fri, 25 Jan 2002 21:10:51 +0000 |
| Subject: | break statement usage | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-82167@lists.php.net to get a copy of this message | ||
Hello, all
I have a quick question about using the "break" statement from within a switch() statement.
After accepting user input from a form, I want to run this input through some error checking via PHP code (not Javascript error checking). So the first thing is the code puts the input through to a couple of error functions (this is all inside of a switch statement that is determined by a radio button on the previous page). If the error functions show the input as invalid, I echo back a specific error-message telling the user which field needs to be fixed, and then "break" to end the case statement. That way the input never comes near the database functions (mysql_query()) if it is invalid input.
Is this a poor way to code -- using the "break" as a shortcut to jump out of the switch statement? Are there other ways that "break" can be used -- such as from loops? I've been learning Python on the side, and in that language, the "break" statement is used often, sometimes in a loop like
while 1
do some code
if condition is true
breakThis is basically an infinite loop until the condition is true -- though I've never seen a WHILE loop in PHP that is formed this way (using a 1 to make it happen infinitely until a condition is met and then BREAKing out of the loop). Usually, at least from what I've seen, PHP WHILE loops are constructed so that they terminate when a condition is met specified immediately after the WHILE, as in: while (x < $number_of_iterations) {
do some code} So what I'm wondering is, Is it bad coding practice to make heavy use of "break" statements in switch() flow control? Thank you for your opinions, Erik