PHP Lessons 6: Break and Continue

by
Inf

Last time we saw how to work with the different kinds of loops: the While, Do While, For and Foreach loops. Today, we are going to see two keywords, “break” and “continue” that you may use to operate on loops and Switch blocks. You can consider this to be a continuation of the Loops lesson.

First, you need to know what those two do:

“break” – if PHP encounters this keyword in a loop, it will immediately stop the loop, and get out of the loop block, whatever the condition is, whether it is returning true or false.

“continue” – if PHP encounters this keyword in a loop, it will stop where it has reached in the loop body, and start another loop without processing the remaining statements.

Optionally, with “break” you can have a parameter that tells it how many nested structures to break out of. We’ll see about that in a moment.

Let’s see how we can use “break” first.

<?php

  $i = 0;

  while ($i < 10){
      $i++;
  }

  echo "The value of i is: " . $i;
?>

The above code increments i up to 10 and echoes “The value of i is: 10” after it ends. It doesn’t make much sense, but it’ll be our example. If I were to use the “break” keyword, I could have written that same code as:

<?php

  $i = 0;

  while(true)
  {
      if ($i == 10){
          break;
      }

      $i++;
  }

  echo "The value of i is: " . $i . "<br />";
?>

Notice above, I have intentionally created an infinite loop when I said “while (true)”. Why is it an infinite loop? Simply because the condition is always true (true is True always) so the loop never ends. I could also have written, “while(1)” for the same effect. But if we continue further, there is a condition that checks when $i gets to 10, then, we go into the body of the IF block. The overall effect is that when $i is 10, the loop will break, and we get our echo.

If we had to break out of a nested loops, say 2 loops, we’d do:

<?php

  for ($i=0; $i<10; $i++){
      for ($j=0; $j<5; $j++){
          //some code here

          if (some condition here){
              break 2;
          }
      }
  }

?>

“break” is also used in the Switch blocks, as we have seen previously, in the Conditions lesson.

Now, for the continue, let’s see how we can get only even numbers from 0 to 10.

<?php

  for ($i=1; $i<=10; $i++){

      if ($i % 2 != 0){
          continue;
      }

  echo "The value of i: " . $i . "<br />";

?>

The code above is to find the even numbers from 1 to 10. There is a For loop to do the 1 to 10 counting. The IF condition inside the loop checks whether $i has an even number inside. How? We use the modulo operator, %. The modulo operator returns the remainder of a division operation. So, if we divide an even number by 2, the remainder is zero. If it’s an odd number, the remainder would be non-zero. That’s what we use here, the non-zero value. If the value is indeed non-zero, we do the “continue;” statement. This tells PHP to stop the loop where it has reached, and resume a new loop.

For e.g. if $i is 3, ($i % 3) returns 1 (a non-zero value) and we go into the IF condition. The continue tells PHP to start a new loop, and $i is now 4. We have effectively skipped the echoing of 3. Just like this, we skip all the odd numbers to keep just the even ones. That’s what continue does – skipping iterations.

What we get as output:

The value of i is: 2
The value of i is: 4
The value of i is: 6
The value of i is: 8
The value of i is: 10

“continue” too has the optional parameter to tell it how many nested loops it should skip out of. Like, “continue 3;” would mean, go to the outer third loop, and restart anew there.

This concludes Lesson 6. In fact, this lesson should have been together with Lesson 5: Loops, but I broke it down because Loops was becoming too long.

[seriesposts title=”PHP Lessons” titletag=h3 listtype=ul orderby=date name=”PHP Lessons” ]