PHP Break and PHP Continue

Updated: September 18th, 2022, 06:35:46 IST
Published: September 18th, 2022
PHP Break and PHP Continue
Title: PHP Break and PHP Continue

Difference between break and continue in PHP: Break ends a loop completely, whereas Continue just shortcuts the current iteration and moves on to the next iteration.

Break Statement in PHP

The break statement can be used to jump out of a loop.


// PHP Break stmt
// Example:

<?php
for ($i = 0; $i < 10; $i++) {
  if ($i == 5) {
    break;
  }
  echo "The number is: $i <br>";
}
?>

This example jumps out of the loop when i is equal to 5.

Continue Statement in PHP

The continue statement in the PHP is used to iterate the loop by skipping the current flow when the condition is satisfied.



// PHP Continue stmt
// Example:

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

This example skips the value of 5, and continues the remaining flow of the loop.

Difference between break and continue in PHP

Both the break and continue statements are used to skip an iteration of a loop. These keywords are useful for regulating the program's flow. The distinction between break and continue is as follows:

  1. The break statement stops the whole loop iteration, but the continue statement skips the current iteration.
  2. The break statement ends the loop early, but the continue statement starts the following iteration early.
  3. In a switch loop, break operates as a case terminator alone, whereas continue to acts as a case terminator and skips the current iteration of the loop.