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: while, do-while, for, 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:
- PHP echoes “While Loop”
- It initializes $i to 0.
- PHP encounters the while keyword.
- 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 0 during the first iteration. Since 0 is less than 5, the expression is evaluated to true. PHP enters the loop body.
- 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).
- 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.”
- 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).
- 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.
- PHP will encounter the continue statement during the 2nd and the 3rd iteration.
- 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).
- 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.
- 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) );
?>