MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP continue Loop


Loops are a fundamental aspect of programming. They allow programmers to create code that repeats for some given number of repetitions, or iterations. The number of iterations can be explicit (6 iterations, for example), or continue until some condition is met ('until Hell freezes over'). This topic covers the different types of loops, their associated control statements, and their potential applications in
PHP.


The continue keyword halts the current iteration of a loop but does not terminate the loop.
Just like the break statement the continue statement is situated inside the loop body. When executed, the continue statement causes execution to immediately jump to the loop conditional.
In the following example loop prints out a message based on the values in an array, but skips a specified value.


$list = ['apple', 'banana', 'cherry'];
foreach ($list as $value) {
 if ($value == 'banana') {
 continue;
 }
 echo "I love to eat {$value} pie.".PHP_EOL;
}

The expected output is:
I love to eat apple pie.
I love to eat cherry pie.
The continue statement may also be used to immediately continue execution to an outer level of a loop by specifying the number of loop levels to jump. For example, consider data such as Fruit Color Cost
Apple Red 1
Banana Yellow 7
Cherry Red 2
Grape Green 4
In order to only make pies from fruit which cost less than 5


$data = [
 [ "Fruit" => "Apple", "Color" => "Red", "Cost" => 1 ],
 [ "Fruit" => "Banana", "Color" => "Yellow", "Cost" => 7 ],
 [ "Fruit" => "Cherry", "Color" => "Red", "Cost" => 2 ],
 [ "Fruit" => "Grape", "Color" => "Green", "Cost" => 4 ]
];
foreach($data as $fruit) {
 foreach($fruit as $key => $value) {
 if ($key == "Cost" && $value >= 5) {
continue 2;
 }
 /* make a pie */
 }
}

When the continue 2 statement is executed, execution immediately jumps back to $data as $fruit continuing the outer loop and skipping all other code (including the conditional in the inner loop



Conclusion

In this page (written and validated by ) you learned about PHP continue Loop . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Functions.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.