PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The continue
keyword in PHP is used within looping structures to skip the rest of the current loop iteration and continue execution at the start of the next iteration.
Let's look at how the continue
statement works in PHP through several examples:
continue
in a for
loop:for($i = 0; $i < 10; $i++) { if($i == 5) { continue; // skips the rest of the code in this loop iteration } echo $i . "<br>"; // this line will not execute when $i is 5 }
In this example, numbers from 0 to 9 will be printed, except for 5. When $i
equals 5, the continue
statement is executed, skipping the rest of the loop's code and starting the next iteration.
continue
in a while
loop:$j = 0; while($j < 10) { $j++; if($j == 5) { continue; // skips the rest of the code in this loop iteration } echo $j . "<br>"; // this line will not execute when $j is 5 }
Similar to the previous example, this while
loop will print numbers from 1 to 10, except for 5. When $j
equals 5, the continue
statement is executed, and the next loop iteration begins.
continue
in a foreach
loop:$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); foreach($numbers as $number) { if($number == 5) { continue; // skips the rest of the code in this loop iteration } echo $number . "<br>"; // this line will not execute when $number is 5 }
In this example, the foreach
loop goes through an array of numbers from 1 to 10. When the current $number
is 5, the continue
statement is encountered, skipping the echo statement and starting the next loop iteration.
The continue
statement can also take a numerical argument to specify the number of levels of enclosing loops it should skip to the end of. continue 2;
would skip to the end of the second enclosing loop, for example. However, this use is less common and can make code harder to understand, so use it with caution.
Remember, the continue
statement is beneficial when you want to skip the rest of the current loop without terminating it completely. It's a handy tool for controlling the flow of your loops in PHP.
How to Use continue
in PHP Loops:
The continue
keyword is used to skip the rest of the current iteration and move to the next iteration of a loop.
<?php for ($i = 1; $i <= 5; $i++) { if ($i == 3) { continue; // Skip iteration when $i is 3 } echo $i . " "; } // Output: 1 2 4 5 ?>
Skip Current Iteration and Move to the Next Loop in PHP:
continue
is useful when you want to skip the current iteration and move to the next loop.
<?php $numbers = [1, 2, 3, 4, 5]; foreach ($numbers as $number) { if ($number % 2 == 0) { continue; // Skip even numbers } echo $number . " "; } // Output: 1 3 5 ?>
PHP continue
Statement in For Loop:
The continue
statement works similarly in a for
loop as it does in other loops.
<?php for ($i = 1; $i <= 5; $i++) { if ($i == 2 || $i == 4) { continue; // Skip iterations when $i is 2 or 4 } echo $i . " "; } // Output: 1 3 5 ?>
Using continue
with While and Do-While Loops in PHP:
continue
can be used with both while
and do-while
loops.
<?php $i = 0; while ($i < 5) { $i++; if ($i == 3) { continue; // Skip iteration when $i is 3 } echo $i . " "; } // Output: 1 2 4 5 ?>
PHP continue
vs break
: Differences and Use Cases:
continue
skips the current iteration and moves to the next, while break
exits the loop entirely.
<?php for ($i = 1; $i <= 5; $i++) { if ($i == 3) { continue; // Skip iteration when $i is 3 } if ($i == 4) { break; // Exit the loop when $i is 4 } echo $i . " "; } // Output: 1 2 ?>
Continue in Nested Loops in PHP:
continue
can be used in nested loops to skip the current iteration in the inner loop.
<?php for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($j == 2) { continue; // Skip inner loop iteration when $j is 2 } echo "$i-$j "; } } // Output: 1-1 1-3 2-1 2-3 3-1 3-3 ?>
PHP continue
Statement with Labeled Loops:
Labeled loops allow you to specify which loop to continue in a nested loop scenario.
<?php outerLoop: for ($i = 1; $i <= 3; $i++) { for ($j = 1; $j <= 3; $j++) { if ($j == 2) { continue outerLoop; // Skip to the next iteration of outer loop } echo "$i-$j "; } } // Output: 1-1 2-1 3-1 ?>
Continue Inside Switch-Case Statements in PHP:
continue
can be used inside a switch-case statement to skip the rest of the current case and move to the next case.
<?php $value = 2; switch ($value) { case 1: echo "Case 1"; break; case 2: echo "Case 2 "; continue; case 3: echo "Case 3"; break; } // Output: Case 2 ?>
PHP continue
with Foreach Loop:
continue
works similarly in foreach
loops, skipping the rest of the current iteration.
<?php $colors = ["red", "green", "blue"]; foreach ($colors as $color) { if ($color == "green") { continue; // Skip iteration when color is green } echo $color . " "; } // Output: red blue ?>
Handling Loop Conditions with continue
in PHP:
Use continue
to handle specific conditions within a loop.
<?php for ($i = 1; $i <= 5; $i++) { if ($i % 2 == 0) { continue; // Skip even numbers } echo $i . " "; } // Output: 1 3 5 ?>
PHP continue
Statement and Loop Control Flow:
continue
affects the control flow of loops, allowing you to skip the current iteration and move to the next one.
<?php for ($i = 1; $i <= 5; $i++) { if ($i == 3) { continue; // Skip iteration when $i is 3 } echo $i . " "; } // Output: 1 2 4 5 ?>