Good day,

Actually, using the "break" statement is the accepted, proper way to break
out of a switch statement, just as it is in other programming languages.

And yes, the "break" statement can be used to terminate other loops, such as
"while" and "for", just like in other languages.  Using it there is not
considered bad programming practice either, although in the case of the
while statement you should try to utilize the loop condition if possible.
FYI, the command to break to the next iteration of a loop is "continue".

The PHP site has very good documentation on the usage and appropriateness
for "switch", "break" and "continue".  I would highly recommend you take a
look, if you have not already.

============================
Darren Gamble
Planner, Regional Services
Shaw Cablesystems GP
630 - 3rd Avenue SW
Calgary, Alberta, Canada
T2P 4L4
(403) 781-4948


-----Original Message-----
From: Erik Price [mailto:[EMAIL PROTECTED]]
Sent: Friday, January 25, 2002 2:11 PM
To: PHP
Subject: [PHP] break statement usage


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
      break

This 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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to