6

I am facing a situation like this (( extracted from php docs ))

Using continue statement in a file included in a loop will produce an error. For example:

 // main.php  

 for($x=0;$x<10;$x++)
 { include('recycled.php'); }

 // recycled.php

 if($x==5)
 continue;
 else 
 print $x;

it should print "012346789" no five, but it produces an error:

Cannot break/continue 1 level in etc.

there is a solution for this??, i mean i need to "process" recycled.php in this way, where the use of continue statement not cause this error, remember this is easy understandable sample code, in the real case i need to find a way to continue the loop of the main.php file. .

7
  • Why don't you make a function (even into another file) that returns a flag, telling you if you have to break or not the cycle? Moreover, this haven't any sense to me: this isn't code reusability, is just wrong approach to what you're trying to do here Commented Jun 7, 2013 at 7:48
  • continue is for loop statements only Commented Jun 7, 2013 at 7:49
  • @DonCallisto, consider this is a sample code, in the real situation case i can't use a function like you suggest cuz i can't access to current variables scope (( where his use is dinamical, and names may vary ))... it is hardy to explain xD Commented Jun 7, 2013 at 7:52
  • @ArunKillu, in the real code case, a script (( tell it "main.php" )) need to execute the code inside of page1.php lot of times in different areas inside main.php Commented Jun 7, 2013 at 8:01
  • You don't do continue in loop! Commented Jun 7, 2013 at 8:05

6 Answers 6

11

You could use return instead of continue within page2.php:

if ($x == 5) {
  return;
}
print $x;

If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call.

PHP: return

Sign up to request clarification or add additional context in comments.

4 Comments

hey!, i think return statement maybe the solution for this, let met test ;)
in this case, return statement will "emulate" the behaviour of continue, accepted!! ;) pD. can edit your answer and leave only the use of return statement?, thanks.
In a sense it does. As the docs state, when returning within an included file, control is passed back to the calling file, so you could execute additional code within the loop after returning from the included file, whereas continue will immediately skip to the next iteration
Brilliant; this worked for me. Was banging my head against walls with break continue and all kinds of workaround. Return did the trick. I too had a for loop and an include inside the for loop. Thanks!
2

Simple NOT include the page for X=5 ?!

for($x=0;$x<10;$x++)
{ 
    if ($x != 5)
        include('page2.php'); 
}

you can not continue, because page2.php is running inside the scope of the include() function, which is not aware of the outer loop.

You can use return instead of continue inside page2.php (this will "return" the include function):

if ($x == 5)
  return;

echo $x;

1 Comment

yes, it solves the problem but @billyonecan post it first, thanks anyway P:
1

As al alternative to using continue, which won't work in included files like that, you could do this in stead:

// page2.php
if($x!=5) {
  // I want this to run
  print $x;
} else {
  // Skip all this (i.e. probably the rest of page2.php)
}

6 Comments

OP's problem is that the included page cannot continue, this is a way a around it. I don't see how that deserves a downvote. I've updated my answer to explain that it's an alternative.
You have a point, I don't understand at once what you'd suggested here. Sorry.
@MaX, in fact this is a solution for this case, but the code posted refers to a sample situation, in the real case i really need use the continue statement.
continue can almost always be replaced like this. It's just a matter of wrapping everything you don't need to run in an else. I'll update to give an example.
To be honest, I'm just covering my a** in case some über geek (and I mean that in the most loving way possible) comes along with an example to prove me wrong. :)
|
0

You can do this also

// page1.php  

 for($x=0;$x<10;$x++)
 { include('page2.php'); }

 // page2.php

 if($x==5)
 { } // do nothing and the loop will continue
 else 
 print $x;

1 Comment

Woulde more conventional to do if($x!=5) print $x; and don't use an else block.
0

you want to continue the loop in included page:

try this:

 for($x=0;$x<10;$x++)
 { 
     $flag = 1;
    if($flag==0){
        continue;
    }
    include('./page2.php'); 
}


if($x==4)
   $flag = 0;
else 
    print $x;

Comments

0

Your code is wrong because you use continue not in a loop! I don't know why you want to include the same file 5 times as well.

for($x=0;$x<10;++$x)
{ 
 //include('page2.php'); 
 if($x!=5)
   print $x;
}

1 Comment

remember, this is sample code to illustrate a situation, the real code "needs" continue the loop of main file in some way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.