Reputation: 759
I have a do while
loop and a nested foreach
loop. And i'm using break 2
to break both(do while
and foreach
) if $x > N
(in this case N = 4
). For now it works but...is this a good practice? Or should i break the while loop in some other way?
$arr = ['a', 'b', 'c'];
$x = 0;
do{
//do something here...
//and something here...
foreach($arr as $k => $v){
if($x > 4){
break 2;
} else{
echo '(' . $x . ')' . $k . ' - ' . $v . '<br />';
}
}
$x++;
} while($x < 10);
Thank you! :D
Upvotes: 1
Views: 1059
Reputation: 47874
Using a do{}while();
loop should be a very deliberate decision. Its behavior forces an initial iteration and only checks the break condition(s) at the end of each iteration.
Compare this to a while()
loop or better for your purposes a for()
loop. It performs the conditional check before each iteration AND the counter, the break condition(s), and the incrementation are all in one readable location.
I'm not sure if you actually mean to execute...
//do something here...
//and something here...
...on the same iteration where the break 2
occurs. What circumstance makes this suitable for your project? The logic of your snippet says, that you want to execute those mystery lines of code when $x = 0, 1, 2, 3, 4, and 5; but the foreach()
block is only to be executed when $x = 0, 1, 2, 3, and 4.
If I am right that your design logic is slightly flawed then, I do not recommend writing a break
point inside your loop with the other processes. I recommend a for()
loop.
$arr = ['a', 'b', 'c'];
for ($x = 0; $x <= 4; ++$x) {
//do something here...
//and something here...
foreach ($arr as $k => $v){
echo "($x)$k - $v<br />";
}
}
Because of your break 2
, your while($x < 10);
condition will never be satisfied, so it is an illogical design inclusion.
If you DO, in fact, need to execute those mystery lines, but not the looped echoes when $x = 5
, then relinquish loop control to the break entirely and use while(true)
(writing an indefinite loop) to remove developer confusion.
$arr = ['a', 'b', 'c'];
$x = 0;
while (true) {
//do something here...
//and something here...
if ($x > 4) {
break 2;
}
foreach ($arr as $k => $v) {
echo "($x)$k - $v<br />";
}
++$x;
}
Upvotes: 1
Reputation: 1196
In your example break 2 will work fine and yes, it is part of PHP. However, I have always had a concern about breaking out of loops especially when these are nested.
Consider if you were working on code that was much more complex that that included nested loops. When you specified the break out of 2 loops, you would need to be very careful that you took account of the breaks in any new code.
So while using break may work, I always to try to avoid breaks and always consider if there is a different option that works without the need for breaks (especially when you have nested loops). In your example, I would use the code:
$arr = ['a', 'b', 'c'];
$x = 0;
do{
//do something here...
//and something here...
foreach($arr as $k => $v){
echo '(' . $x . ')' . $k . ' - ' . $v . '<br />';
}
$x++;
} while($x <= 4);
This usually leads to neater code that is easier to maintain.
Upvotes: 0