PHP Continue Statement

Conducting Loops in Harmony with Continue

We looked at the break statement in the previous article. As a refresher, the break statement terminates the loop once it’s encountered. Sometimes we want to just skip an iteration and continue with the loop; that can be achieved with the continue statement.

Let’s look at how we can use the continue statement inside of each loop: whiledo-whilefor, and foreach. Each loop will iterate through the following array:

?php

$next_car = [
    "Porsche 911",
    "Ferrari F355",
    "Mitsubishi EVO",
    "R34 Skyline",
    "C8 Corvette"
];

?>

In the first example, we’ll look at the while loop. PHP will iterate through the $next_car array; once it matches “R34 Skyline,” PHP will output “That’s right. I’m getting a Skyline.”

<?php

echo "While Loop<br>";

$i = 0;

while ( $i < count($next_car) ) {

    if ( $next_car[$i++] != "R34 Skyline" ) {
        echo $i;
        continue;
    }

    echo "That's right. I'm getting a skyline.<br>";
}

?>

As is customary in most of my articles, I like to walk through at least one example fully. I think it provides the reader with a clear-cut way of looking at the code. In this example:

  1. PHP echoes “While Loop”
  2. It initializes $i to 0.
  3. PHP encounters the while keyword.
  4. It looks at the condition $i < count($next_car). The function count($next_car) returns 5 since there are 5 elements in the array $next_car. The variable $i contains the value during the first iteration. Since is less than 5, the expression is evaluated to true. PHP enters the loop body.
  5. The if statement is encountered. PHP checks to make sure that the current element does not equal “R34 Skyline.” During the first iteration, $next_car[$i++] returns “Porsche 911.” As soon as that statement is processed, $i is iterated from 0 to 1 (remember how the post-increment operator works).
  6. PHP checks to see if “Porsche 911” equals “R34 Skyline.” Since it doesn’t, the statement evaluates to true due to the != operator. It’s saying, “Is it true that Porsche 911 does not equal R34 Skyline?” And PHP responds “Yes, that’s true.”
  7. Since the if statement returned true, PHP enters the if statement body and echoes out $i (this is just to show you that $i was incremented).
  8. The next statement that PHP encounters is the continue statement. The continue statement tells PHP to skip the remainder of the code that’s inside of the while loop body and go back to the top to test the while loop conditional. So, go back to step 4 and repeat.
  9. PHP will encounter the continue statement during the 2nd and the 3rd iteration.
  10. During the 4th iteration, the if statement conditional evaluates to false, therefore the if statement is skipped. PHP reaches the echo statement and outputs “That’s right. I’m getting a Skyline.” This would be a perfect place to add a break statement to exit the loop, but we’ll keep going since there could be multiple elements inside of the array that contain the value “R34 Skyline” (and because we’re not covering the break statement here).
  11. Because there are more elements in the array, and nothing was done to terminate the loop, it keeps going until it reaches the last element. Each time, the expression returns true inside of the if statement conditional and the continue statement is executed.
  12. When $i is incremented from 5 to 6, PHP evaluates the expression inside of the while loop, and it returns false. PHP exits the loop.

Next, let’s look at the do-while loop. PHP evaluates the statements inside of the do-while body before it tests the do-while condition. Ultimately, the same result is produced. PHP will encounter the continue statement each time that “R34 Skyline” is not returned. Once it’s returned, PHP will output “That’s right. I’m getting a Skyline.”

<?php

echo "Do While Loop<br>";

$i = 0;

do {
    if ( $next_car[$i++] != "R34 Skyline" ) {
        echo $i;
        continue;
    }

    echo "That's right. I'm getting a skyline.<br>";
} while( $i < count($next_car) );

?>

Let’s mix it up a little with the for loop example. We’ll add “Subaru STi” to the array, which just happens to be a car that I already own. We want to loop through the $next_car array and output the message: “The BLANK-CAR is definitely a possibility…with enough subscribers.” BLANK-CAR references each array element value. If we encounter Subaru STi, we don’t want to output anything since we already own it (or at least I already own it). So, if PHP encounters that value, it should skip over it using the continue statement.

<?php

$next_car = [
    "Porsche 911",
    "Subaru STi",
    "Ferrari F355",
    "Mitsubishi EVO",
    "R34 Skyline",
    "C8 Corvette"
];

echo "For Loop<br>";

for ( $i = 0; $i < count($next_car); $i++ ) {

    if ( $next_car[$i] == "Subaru STi" ) {
        continue;
    }

    echo "The " . $next_car[$i] . " is definitely a possibility...with enough subscribers.<br>";
}

?>

The code achieves just that. It iterates through each array element. If the value matches “Subaru STi,” the continue statement is encountered and PHP skips the remaining code inside of the loop. It goes back to the top and repeats the process. Otherwise, the continue statement is not encountered and the code below the if statement is executed.

Let’s look at the last loop: the foreach loop. The foreach loop will be identical to the for loop in logic (only the syntax will differ).

<?php

echo "Foreach Loop<br>";

foreach( $next_car as $car ) {

    if ( $car == "Subaru STi" ) {
        continue;
    }

    echo "The " . $car . " is definitely a possibility...with enough subscribers.<br>";
}

?>

And that’s it. That’s how you use the continue statement inside each of the loops. The process is exactly the same for each (no pun intended) loop.

PHP Tutorial Series

Continue your learning with these articles

PHP Break Statement

LOOP LIBERATOR: HARNESSING BREAKS FOR PRECISION AND PROGRESS

PHP – P33: break statement

We looked at the break statement when we covered switch statements. In that case, the break statement was used to end the execution of the switch statement. The break statement isn’t exclusive to the switch statement: it can be used in loops as well.

PHP Continue Statement

Conducting Loops in Harmony with Continue

PHP – P34: Continue Statement 

Need to skip something in a loop? Use the continue statement to do so. Breaks exit loops and continues skip over a single iteration.

PHP User Defined Functions

EMPOWERING CODERS TO SHAPE LOGIC THEIR WAY

PHP – P35: USER DEFINED FUNCTIONS

Functions can be a difficult topic to grasp if you’re a new programmer. I’m sure you were exposed to functions in math, but do you really understand what the term “function” means?

Leave a Reply