PHP continue statement
break statement is used to terminate the loop whenever require. Similarly we can use continue statement to skip the part of the loop.
PHP continue statement :
The continue statement forces the next iteration of the loop skipping the remaining code of the loop.
- In the case of the for loop as soon as after the execution of continue statement, increment/decrement statement of the loop gets executed. After the execution of increment statement, condition will be checked.
- In case of the while loop, continue statement will take control to the condition statement.
- In case of the do..while loop, continue statement will take control to the condition statement specified in the while loop.
Example #1. Continue statement :
<html> <body> <?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); foreach( $array as $value ) { if( $value == 3 ) continue; echo "Value is $value <br />"; } ?> </body> </html>
Output :
Value is 1 Value is 2 Value is 4 Value is 5 Value is 6 Value is 7 Value is 8 Value is 9 Value is 0
In the above example, when value becomes 3 then the echo statement gets skipped.
Explanation :
In this example we have created an array of the number using the following line -
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
Now using the for each loop we are iterating the array. In each iteration we have selected an element from the array -
foreach( $array as $value )
When $value is equal to 3 then we are skipping the remaining part of the loop and control goes to check condition before executing the next iteration.