Forum Moderators: coopster

Message Too Old, No Replies

break; problem

break

         

cody7

11:03 pm on Apr 12, 2005 (gmt 0)

10+ Year Member



<?
function stop1(){
break;
}

for($x=0; $x<70; $x++){
echo "THE X = ".$x."<br>";
for($y=0; $y<30; $y++){
echo ">>>>>>>>THE Y = ".$y."<br>";
if($y==10){

stop1(); //- I use the function stop1();

}
}
}
?>

my error is:

Fatal error: Cannot break/continue 1 level in C:\apachefriends\xampp\htdocs\john\URLParser\gui.php on line 33

but if i use

<?
function stop1(){
break;
}

for($x=0; $x<70; $x++){
echo "THE X = ".$x."<br>";
for($y=0; $y<30; $y++){
echo ">>>>>>>>THE Y = ".$y."<br>";
if($y==10){

break; //- direct call of break;

}
}
}
?>

my program goes well....

i need my break command to be in a function...

what is the cause of the fatal error and how to resolve this. thanks for any help...

coopster

11:23 pm on Apr 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



break [php.net] ends execution of the current for, foreach, while, do-while or switch structure -- not a function. To exit a function you need to use return [php.net].

mcibor

9:14 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot have break in a function, because it tries to stop the function, not the loop where the function is called. It's because the break is not the whole code. It's always asigned to the nearest loop.

I don't recon there's a way to put that break in another function.

Maybe you could:

function stop()
{ return "break;";}

while(1){ eval(stop());}

But this is extremely ugly for a programmer! And I don't recommend this usage.

Best regards
Michal Cibor