What does the number after "break" or "continue" mean in PHP?

Can someone explain, with examples, what is meant by a break 2 or continue 2 loop in PHP? What does it mean when break or continue followed by a number?

+47
loops php break continue breakpoints
Mar 02 2018-11-12T00:
source share
4 answers
 $array = array(1,2,3); foreach ($array as $item){ if ($item == 2) { break; } echo $item; } 

prints "1" because the loop was broken forever until the echo could print "2".

 $array = array(1,2,3); foreach ($array as $item){ if ($item == 2) { continue; } echo $item; } 

outputs 13 because the second iteration passed

 $numbers = array(1,2,3); $letters = array("A","B","C"); foreach ($numbers as $num){ foreach ($letters as $char){ if ($char == "C") { break 2; // if this was break, o/p will be AB1AB2AB3 } echo $char; } echo $num; } 

infers AB due to break 2 , which means that both statements were broken pretty early. If it was just break , the output would be AB1AB2AB3 .

 $numbers = array(1,2,3); $letters = array("A","B","C"); foreach ($numbers as $num){ foreach ($letters as $char){ if ($char == "C") { continue 2; } echo $char; } echo $num; } 

ABABAB outputs, due to continue 2 : each loop will be transmitted by an outer loop.

In other words, continue stops the current iteration, but allows another to run, and break stops the whole statement completely.
Thus, we can use that continue is only applicable for loops, while break can be used in other statements, such as switch .

A number represents the number of dependent nested .
if there are 2 nested loops, break in the inner will break the inner inner (however, this makes little sense, since the inner loop will start again in the next iteration of the outer loop). break 2 in the inner loop will break both.

+76
Mar 02 2018-11-12T00:
source share

The number simply says โ€œhow many areas to jump out ofโ€

 <?php for($i = 0; $i < 10; ++$i) { for($j = 0; $j < 10; ++$j) { break 2; } } 

$ i and $ j will be 0

To quote the manual:

continue takes an optional numeric argument that tells it how many levels of enclosing loops it should skip to the end.

same thing for a break.

+32
Mar 02 2018-11-12T00:
source share

break takes an optional numeric argument that tells how many of the nested layout structures should be broken.

 <?php $arr = array('one', 'two', 'three', 'four', 'stop', 'five'); while (list(, $val) = each($arr)) { if ($val == 'stop') { break; /* You could also write 'break 1;' here. */ } echo "$val<br />\n"; } /* Using the optional argument. */ $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5<br />\n"; break 1; /* Exit only the switch. */ case 10: echo "At 10; quitting<br />\n"; break 2; /* Exit the switch and the while. */ default: break; } } ?> 

Additional break examples

continue takes an optional numeric argument that tells it how many levels of enclosing loops it should skip to the end. The default value is 1, thus skipping to the end of the current loop.

 <?php while (list($key, $value) = each($arr)) { if (!($key % 2)) { // skip odd members continue; } do_something_odd($value); } $i = 0; while ($i++ < 5) { echo "Outer<br />\n"; while (1) { echo "Middle<br />\n"; while (1) { echo "Inner<br />\n"; continue 3; } echo "This never gets output.<br />\n"; } echo "Neither does this.<br />\n"; } ?> 

More examples continue

+9
Oct 09
source share

break: break the inner loop (exit the loop)

break 2: break 2 loops of the nesting level (exit from 2 nested loops)

continue: forced loop for the next iteration from where it is used, without executing the rest of the loop code

continue 2: forced loop for the next 2 iterations from where it is used, without executing the loop stop code

exit the loop when we meet $array value 5

  break $array(4,5,8); for ($i=0 ;$i < 10 $i ++) { if ($array[$i]==5) { break; } } 

break (n)

Exit both loops when we meet the value 5 in $ array;

 for ($i=0 ;$i < 10 $i ++) { for($j=0; $j <10; $j++) { if ($array[$i][$j]==5) { break 2; } } } 

continue

Prints a message when the value is 5;

 for($i=0; $i<10; $i++) { if ($array[$i] != 5) { continue;// will reach at the first line from here which is for($i=0;..... } echo 'This is five'; } 

}

+3
Mar 02 '11 at 12:48
source share



All Articles