What is meant by a number after "break" or "continue" in PHP?

57,415

Solution 1

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

outputs "1" because the loop was broken forever, before echo was able to print "2".

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

outputs 13 because the second iteration was 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;
}

outputs AB because of break 2, which means that both statements was broken quite early. If this was just break, the output would have been 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;
}

will output ABABAB, because of continue 2: the outer loop will be passed every time.

In other words, continue stops the current iteration execution but lets another to run, while break stops the whole statement completely.
So we can ell that continue is applicable for the loops only, whereas break can be used in other statements, such as switch.

A number represents the number of nested statements affected.
if there are 2 nested loops, break in the inner one will break inner one (however it makes very little sense as the inner loop will be launched again in the next iteration of the outer loop). break 2 in the inner loop will break both.

Solution 2

The number just says "how many scopes 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 accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.

same goes for break.

Solution 3

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

<?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;
    }
}
?>

More examples of break

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. 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 of continue

Solution 4

break : break the inner most loop (exit from the loop)

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

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

continue 2: force loop for next 2 iteration from where it is used without executing rest of loop code

exit the loop when we encounter $array value to be 5

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

break (n)

Exit both loops when we encounter value 5 in $array;

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

continue

Will print the message when 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';
}

}

Share:
57,415
kn3l
Author by

kn3l

rust developer

Updated on July 09, 2022

Comments

  • kn3l
    kn3l almost 2 years

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

  • n8jadams
    n8jadams almost 5 years
    I prefer your answer simply because it is short and to-the-point.