Understanding the PHP foreach continue with Example
Summary: In this tutorial, we will learn the PHP foreach continue in details with example. Let’s understand the program.
PHP foreach continue
It is the most-used function in my life with PHP is foreach. furthermore, because it’s just an exquisitely named function, and it maps well to how I think. Partly this is because it’s the type of the backbone of a webpage: iterate through a group of things, operating on some selectively, and so show all of them.
you really must know foreach. And so that’s our goal: covering the most common and useful parts of foreach during a method that we tend to hope makes it straightforward for you.
PHP continue statement is utilized within looping structures to skip the rest of the current loop iteration and continue execution at the condition analysis or evaluation and then the starting or beginning of the next iteration.
|
1 2 3 4 5 6 7 8 9 |
<?php $number = array ("January", "Febuary", "March", "April", "May", "Jun", "July"); foreach ($number as $element) { if ($element == "Stop") { continue; } echo "$element </br>"; } ?> |
Febuary
March
April
May
Jun
July
PHP continue statement accepts an optional numeric argument that tells it how many levels of enclosing loops it should skip to the end of. furthermore, The default value is 1, thus skipping to the end of the current loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php foreach ($arr as $key => $value) { if (!($key % 2)) { // skip even members continue; } do_something_odd($value); } $i = 0; while ($i++ < 5) { echo "Outer<br />\n"; while (1) { echo "Middle<br />\n"; while (1) { echo "Inner<br />\n"; continue 3; } echo "This never gets output.<br />\n"; } echo "Neither does this.<br />\n"; } ?> |
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Omitting the semicolon (punctuation mark) after continue can lead to confusion. Here is an example of what you should not do.
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php // defines the for loop for ($x = 0; $x < 9; $x++) { if ($x == 5) { echo " Skipped number is $x <br>"; // prints the skipped number. continue; /* It skips the defined statement if the condition is true. */ } echo "Number is: $x <br>"; } ?> |
Number is: 1
Number is: 2
Number is: 3
Number is: 4
Skipped number is 5
Number is: 6
Number is: 7
Number is: 8
Conclusion:
In this article, you have learned the PHP foreach continue in details with example. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!