tags:

views:

146

answers:

7

Hi, is it good practice to use break and continue as sentinel for loops in PHP?

e.g.

if (!empty($var))
    break;
A: 

Let me answer with a couple of questions:

  • Why shouldn't it be considered good practice?

  • Does it reduce readability of your code?

  • Does it slow down your for cycles?

Juraj Blahunka
+1  A: 

In light usage it is ok, but in heavy usage it makes your code spaghetti. break and continue is basically just a restricted goto and as such, use sparingly.

Earlz
Except for the fact that with goto you never know if it's jumping backward or forward. With break and continue you know where it jumps
jab
A: 

It absolutely is, they're both valid programming constructs.

What is not a good idea is the newly introduced GOTO. (Please tell me this was an April fool's joke I didn't see the note about!)

Pekka
PHP has `goto`!!?
Earlz
When the php.net page includes an XKCD comic, you've really got to wonder
Erik
Yeah. I mean, what do you need when a programming language already has an image of unprofessionality? You add *goto*. Sheesh.
Pekka
@Erik I am still somehow thinking this was an April fool's joke!
Pekka
A: 

In Python, we use infinite loop and break to improve readability. So yes, you can use it, provided you do it correctly, and for a good reason.

e-satis
+3  A: 
do {
 if (condition1)
   break;
 some code;
 some code;
 if (condition2)
   break;
 some code;
 some code;
 if (condition3)
   break;
 some code;
 some code;
} while (false);

vs.

if (!condition1) {
   some code; 
   some code;
   if (!condition2) {
      some code;
      some code;
      if (!condition3) {
         some code;
         some code;
      }
}

Some find the first version an abhomination and difficult to read and love the second version. Some find the first version cleaner and easier to read. As the number of conditions multiply, I tend to find the first version easier to follow, as the second one tends to get more and more difficult to follow the level of nesting. Also if the if (condition) break; gets into something only slightly more complex like if (condition) {some code; break}, the do {if .. break; if .. break..;} while(false) pattern gets even more clear compared with equivalend nested ifs.

Remus Rusanu
A: 

For a discussion of break and continue in PHP (and looping in general) have a look at Advanced loops - you get the impression that the author can just about manage to swallow break and continue but not break n and continue n. :-)

mikej
A: 

In 5+ years programming PHP I never had to use break outside of switch statements.

Continue is sometimes used to skip first or last items in iterations, but I don't like it very much.

Why did they reintroduce GOTO ? that's a shame

To answer the question, in

if (!empty($var))
    break;

Why not use return (if in a method context)

if (!empty($var))
    return false;

I think this way much clear and makes the caller aware of what's happened inside. A better use for argument errors is using Excecptions, which in facte will break execution a the point they are raised.

Benoit