Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php, include file who uses continue statement

Tags:

php

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. .

like image 323
AgelessEssence Avatar asked Dec 27 '22 02:12

AgelessEssence


2 Answers

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

like image 129
billyonecan Avatar answered Jan 02 '23 23:01

billyonecan


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;
like image 38
dognose Avatar answered Jan 02 '23 22:01

dognose