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. .
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With