break and continue Statement in PHP

This article is created to cover the two important statements or keywords in PHP, which are:

  1. break
  2. continue

PHP break statement

The PHP break keyword or statement is used when we need to exit from the current loop or switch structure. For example:

<?php
   for($i=1; $i<10; $i++)
   {
      if($i>5)
         break;
      
      echo $i;
      echo "<BR>";
   }
?>

The snapshot given below shows the output produced by this PHP example on the break statement:

php break keyword

In the above example, when the value of $i becomes greater than 5, that is, when the value of $i becomes 6, then the condition $i>5 or 6>5 evaluates to be true, and therefore, the program flow goes inside the if block and the "break;" statement gets executed, which skips the remaining execution of the current for loop. Therefore, we only get the output from 1 to 5.

The break statement skips the execution of the remaining statement(s) (if any) along with all the remaining iterations of the current loop.

The break statement can also be used to stop the execution of an infinite loop (the loop whose condition always evaluates to true). For example:

<?php
   $num = 10;
   while(true)
   {
      echo $num, "<BR>";
      
      if($num==12)
         break;
      
      $num++;
      echo $num, "<BR>";
   }
?>

The output of this PHP example should be:

php break statement

See the condition of the loop; it is true, which obviously evaluates to being true forever. Therefore, using the break statement, the execution of this loop ends.

The following is the dry run of the preceding example:

PHP continue statement

The PHP continue statement or keyword is used when we need to skip the execution of remaining statement(s) for the current iteration of the current loop and continue for the next iteration of the current loop. For example:

<?php
   for($i=1; $i<10; $i++)
   {
      if($i==5)
         continue;
      
      echo $i;
      echo "<BR>";
   }
?>

The output of the above PHP example on the continue statement is:

php continue keyword

In the above example, when the value of $i becomes 5, the condition $i==5 evaluates to be true. Therefore, the continue statement gets executed, which forces the program to jump and continue the execution of the next iteration of the current loop. That is, the two echo statements get skipped for that iteration.

Difference between break and continue in PHP

The break is used to skip all the remaining executions and iterations of the current loop. where the continue keyword is used to skip the execution of the remaining statement(s) of the current iteration of the current loop. For example:

<?php
   echo "<h2>Using the break Statement</h2>";
   for($i=1; $i<=5; $i++)
   {
      if($i==3)
         break;
      echo $i, "<BR>";
   }
   
   echo "<h2>Using the continue Statement</h2>";
   for($i=1; $i<=5; $i++)
   {
      if($i==3)
         continue;
      echo $i, "<BR>";
   }
?>

The output of the above PHP example is:

php break continue difference

PHP Online Test


« Previous Tutorial Next Tutorial »