PHP Break and Continue

PHP Break

As We have seen in switch case chapter that break statement is used to jump out of switch statement.

The break statement is also used to jump out of loop statement.

<?php  
for ($n = 0; $n <= 6; $n++) {
  if ($n == 2) {
    break;
  }
  echo "The number is: $n <br>";
}
?>
<?php  
$n = 0;
 
while($n < 6) {
  if ($n == 2) {
    break;
  }
  echo "The number is: $n <br>";
  $n++;
} 
?> 

PHP Continue

If a stated condition happens, the continue statement breaks one iteration (in the loop) and continues with the following iteration in the loop.

<?php  
for ($n = 0; $n < 50; $n+=10) {
  if ($n == 20) {
    continue;
  }
  echo "The number is: $n <br>";
}
?>

Now we will see example of continue; in while loop.

<?php  
$n = 0;
 
while($n < 50) {
  if ($n == 20) {
    $n+=10;
    continue;
  }
  echo "The number is: $n <br>";
$n+=10;
} 
?>