The continue Statement in PHP

Sheeraz Gul Feb 06, 2022
  1. Use the continue Statement in PHP while and do-while Loop
  2. Use the continue Statement in PHP for and foreach Loop
  3. Use the continue Statement With Parameters in PHP
The continue Statement in PHP

The continue statement is a conditional statement inside any loop to skip the current iteration based on the provided condition.

The loop will jump to the next iteration if it satisfies the condition with the continue statement.

The continue statement is commonly used in PHP conditions inside the loops, including while, do-while, for, and foreach loops.

The continue statement controls the flow to manipulate the code however you want.

Use the continue Statement in PHP while and do-while Loop

<?php
// While Loop
$demo = 0;
echo" The output for while loop<br>";
while($demo < 5) {
    if ($demo == 2) {
        echo "The number 2 is skipped<br>";
		$demo++;
        continue;
    }
    echo "$demo <br>";
    $demo++;
}

//Do-While Loop
$demo = 0;
echo"<br>The output for do-while loop<br>";
do {
    echo "$demo<br>";
    $demo++;
    if($demo==3){
        echo"The number 3 is skipped<br>";
        $demo++;
        continue;
    }
}
while ($demo < 5);
?>

Output:

The output for while loop
0
1
The number 2 is skipped
3
4

The output for do-while loop
0
1
2
The number 3 is skipped
4

As we can see, the numbers 2 and 3 were skipped from both loops because the condition satisfies, and the continue statement immediately jumps to the next iteration.

Use the continue Statement in PHP for and foreach Loop

For loop output is similar to the while loop; see example:

<?php  
for ($demo = 0; $demo < 5; $demo++) {
    if ($demo == 2) {
        continue;
    }
    echo "$demo <br>";
}
?>

The iteration with number 2 will be skipped.

Output:

0
1
3
4 

Similarly, in foreach, the continue statement can skip the iteration based on the particular value or key of the array. See example:

<?php  
$demo_arr = array('USA', 'China', 'Russia', 'France', 'Germany');

foreach($demo_arr AS $value){
    if($value == 'Russia'){
        continue;
    }
    echo $value.'<br>';
} 
?>

The iteration with the value Russia will be skipped.

Output:

USA
China
France
Germany

Similarly, the continue statement can also be used in other conditions like else, elseif, and switch.

Use the continue Statement With Parameters in PHP

The continue takes one parameter used for the multiple level loops. See example.

<?php
for ($first = 0;$first<3;$first++) {
    echo "Start Of First Loop<br>";
    for ($second=0;;$second++) {
        if ($second >= 2) continue 2; // This "continue" will apply to the "$first" loop
        echo "First Loop = $first Second Loop = $second"."<br>";
    }
    echo "End<br>";
}
?> 

As you can see, the continue is in the second loop, but with parameter 2, if there is no parameter, the continue will directly work on the first loop.

Output:

Start Of First Loop
First Loop = 0 Second Loop = 0
First Loop = 0 Second Loop = 1
Start Of First Loop
First Loop = 1 Second Loop = 0
First Loop = 1 Second Loop = 1
Start Of First Loop
First Loop = 2 Second Loop = 0
First Loop = 2 Second Loop = 1

continue is a keyword in PHP, and it can’t be used as a variable name inside the loop in which the statement is used; otherwise, it will show an error.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Statement