In PHP, the continue
statement is used inside loops to skip the current iteration and move to the next one. It is useful when you want to ignore certain values without breaking the entire loop.
🛠 Syntax of Continue Statement
<?php continue; // Skips the current iteration ?>
📝 Example 1: Continue in a For Loop
This loop skips number 3 and continues with the next iteration.
<?php for ($i = 1; $i <= 5; $i++) { if ($i == 3) { continue; // Skips iteration when $i is 3 } echo "Number: $i <br>"; } ?>
📝 Example 2: Continue in a While Loop
The loop skips number 4 and proceeds with the next iteration.
<?php $count = 0; while ($count < 6) { $count++; if ($count == 4) { continue; // Skip when $count is 4 } echo "Count: $count <br>"; } ?>
📝 Example 3: Continue in a Foreach Loop
Skips "Cherry" while iterating through the array.
<?php $fruits = array("Apple", "Banana", "Cherry", "Mango"); foreach ($fruits as $fruit) { if ($fruit == "Cherry") { continue; // Skips "Cherry" } echo "Fruit: $fruit <br>"; } ?>
🎯 When to Use Continue?
- To skip unwanted values without stopping the loop.
- To improve performance by avoiding unnecessary processing.
- To filter specific values while iterating through data.
📝 Practice Time!
Modify the examples and experiment with different values to understand how continue works!