1

http://php.net/manual/en/control-structures.continue.php

Changelog says that as of 5.4, following change happened: Removed the ability to pass in variables (e.g., $num = 2; continue $num;) as the numerical argument.

Why on earth would they do that?

So, basically, this is now invalid:

for ($i = 0; $i < 10; $i++) {
    $num = 5;
    continue $num;
}

Am I understanding this correctly? Why would they do that? I just cannot think of a reason.

The Onin
  • 5,068
  • 2
  • 38
  • 55
  • 1
    That's not what `continue` does. It skips loop scopes, not iterations within a loop. – mario Nov 19 '14 at 04:36
  • I'm also lacking this feature. I want to skip some number of loops and then process the rest. With the dynamic argument removed from ***continue;*** I cannot do it. – Tarik Sep 06 '17 at 06:11

2 Answers2

1
$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";
}

here in the above example from PHP Manual the continue skips the echo "This never gets output.<br />\n"; and echo "Neither does this.<br />\n"; statement and the continue 3; denotes the loop number to continue

for ($i = 0; $i < 10; $i++) {
    $num = 5;
    continue ;
    echo $num;
}

the above will skip the printing of $num

Ronser
  • 1,855
  • 6
  • 20
  • 38
  • couldn't get what you are saying?? – Ronser Nov 19 '14 at 04:41
  • Alright man, thanks for answering, I guess I never really understood this "language construct". Also, what I think Mario was saying is that you should've puta reference to your example (i.e. say that you copied it from http://php.net/manual/en/control-structures.continue.php). Thanks again from me, gonna mark as accepted. – The Onin Nov 19 '14 at 04:55
  • Did this answer the question? – Mark Miller Nov 19 '14 at 04:56
  • What is the relation of this answer with the question!? I'm having problems being not able to pass a dynamic variable to continue and this answer does not solve it. – Tarik Sep 06 '17 at 06:08
0

Searching for an alternative function or similar for being able to pass a dynamic variable to continue; and the accepted answer is nowhere related with the question,

I found a workaround that works instead:

$totalLoops = 20;
$skipLoops = 5;
$cnt = 0;
while ($cnt < $totalLoops){
    ++$cnt;  
    if ($cnt <= $skipLoops){continue;}
    echo "Next number is: $cnt<br>";
}

Change the $skipLoops to how many loops you want to skip and you have an alternative of dynamic continue.

Tarik
  • 4,270
  • 38
  • 35