Welcome! Log In Create A New Profile

Advanced

[PHP] Break or continue an outer loop from an inner loop?

Posted by Jeffry Killen 
Jeffry Killen
[PHP] Break or continue an outer loop from an inner loop?
December 05, 2017 08:40PM
Hello;

I have read this early on in texts I have used but have never used it.

Now I have a project that looks like it is appropriate.

The code starts a loop and inside the loop for each iteration
there is an inner loop.

What I need to do is: depending on a conditional, I want to
tell the outer loop to continue from the inner loop: I.E.
stop the inner loop iteration and have the outer loop jump
to its next iteration.

I remember something about labels of some other symbol
associated with each loop and can specify that symbol.

But I don't remember the details.

Can someone guide me with this, or point me to the
applicable part of the manual?

Thanks for time and attention
JK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
Re: [PHP] Break or continue an outer loop from an inner loop?
December 05, 2017 08:50PM
On 5 December 2017 19:33:15 GMT+00:00, Jeffry Killen <[email protected]> wrote:
>Hello;
>
>I have read this early on in texts I have used but have never used it.
>
>Now I have a project that looks like it is appropriate.
>
>The code starts a loop and inside the loop for each iteration
>there is an inner loop.
>
>What I need to do is: depending on a conditional, I want to
>tell the outer loop to continue from the inner loop: I.E.
>stop the inner loop iteration and have the outer loop jump
>to its next iteration.
>
>I remember something about labels of some other symbol
>associated with each loop and can specify that symbol.
>
>But I don't remember the details.
>
>Can someone guide me with this, or point me to the
>applicable part of the manual?
>
>Thanks for time and attention
>JK

If you just want to stop the inner loop and continue the outer one, a simple break will do:

for($i=0; $i<10; $i++)
{
for($j=0; $j<10; $j++)
{
echo "$i, $j\n";

if($j==6)
break;
}
}

Thanks,
Ash
Thanks,
Ash

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Harkness
Re: [PHP] Break or continue an outer loop from an inner loop?
December 05, 2017 09:10PM
On Tue, Dec 5, 2017 at 11:33 AM, Jeffry Killen <[email protected]> wrote:

> The code starts a loop and inside the loop for each iteration
> there is an inner loop.
>
> What I need to do is: depending on a conditional, I want to
> tell the outer loop to continue from the inner loop: I.E.
> stop the inner loop iteration and have the outer loop jump
> to its next iteration.


From the docs for continue [1]:

continue accepts an optional numeric argument which tells it how many
levels of enclosing loops
it should skip to the end of. The default value is 1, thus skipping to
the end of the current loop.

$i = 0;
while ($i++ < 5) {
echo "Outer<br />\n";
while (1) {
echo "Middle<br />\n";
while (1) {
echo "Inner<br />\n";
continue 3; // <-- exit outermost while loop
}
echo "This never gets output.<br />\n";
}
echo "Neither does this.<br />\n";
}

The same applies to break, even in a switch statement.

On Tue, Dec 5, 2017 at 11:45 AM, Ashley Sheridan <[email protected]>
wrote:

> If you just want to stop the inner loop and continue the outer one, a
> simple break will do:
>
> for($i=0; $i<10; $i++)
> {
> for($j=0; $j<10; $j++)
> {
> echo "$i, $j\n";
>
> if($j==6)
> break;
> }
> }


This example works, but it will break if the outer loop has more lines of
code following the inner loop.

Cheers!
David

[1] http://php.net/manual/en/control-structures.continue.php
Jeffry Killen
Re: [PHP] Break or continue an outer loop from an inner loop?
December 05, 2017 09:30PM
> On Dec 5, 2017, at 12:04 PM, David Harkness <[email protected]> wrote:
>
> On Tue, Dec 5, 2017 at 11:33 AM, Jeffry Killen <[email protected]> wrote:
> The code starts a loop and inside the loop for each iteration
> there is an inner loop.
>
> What I need to do is: depending on a conditional, I want to
> tell the outer loop to continue from the inner loop: I.E.
> stop the inner loop iteration and have the outer loop jump
> to its next iteration.
>
> From the docs for continue [1]:
>
> continue accepts an optional numeric argument which tells it how many levels of enclosing loops
> it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.
>
> $i = 0;
> while ($i++ < 5) {
> echo "Outer<br />\n";
> while (1) {
> echo "Middle<br />\n";
> while (1) {
> echo "Inner<br />\n";
> continue 3; // <-- exit outermost while loop
> }
> echo "This never gets output.<br />\n";
> }
> echo "Neither does this.<br />\n";
> }
>
> The same applies to break, even in a switch statement.
>
> On Tue, Dec 5, 2017 at 11:45 AM, Ashley Sheridan <[email protected]> wrote:
> If you just want to stop the inner loop and continue the outer one, a simple break will do:
>
> for($i=0; $i<10; $i++)
> {
> for($j=0; $j<10; $j++)
> {
> echo "$i, $j\n";
>
> if($j==6)
> break;
> }
> }
>
> This example works, but it will break if the outer loop has more lines of code following the inner loop.
>
> Cheers!
> David
>
> [1] http://php.net/manual/en/control-structures.continue.php
>

Thank you all for the responses.

I do have code after the inner loop. I should have mentioned they are simple for loops.
But my project is generating table markup for a year calendar.
The outer loop produces the rows, while the inner loop produces the cells in each row,
As I thought of this, it occurred to me that breaking out of the inner loop will produce a
row with less than 7 cells. So I actually shouldn't do it here.

I am having difficulty getting the month day numbers to fall on the correct week days.

I would post what I have on my web site, but alas, my site doesn't have a hosting service
at this time. (any suggestions for a reasonably priced, reliable, unix based hosting service
in the US?)


The process of contemplating and posting about issues is a process of clarifying
the issues so solutions tend to emerge regardless of the responses. The responses
are most often helpful.

Thank you again for time and attention
JK
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login