...

View Full Version : PHP Equivilent to Javas break;



Ultragames
10-16-2004, 06:57 PM
In Java, you can break out of a while loop or for loop whose ending paramiter have not yet been met with the break; command.

How can i break out of an if and while loop in PHP? For instance:

while($i < 37){
print"blah";
if($the == true){
break;
}
}

Im not sure if you can break out of an if, and a while, because once you break out of the if, it wouldnt read another break...

Anyways. If there is some form of a break command for PHP, that would be nice. And is there a way to use an if statement to get out the while, besides in this example, setting $i to 37?

raf
10-16-2004, 07:18 PM
did you try the code you posted? It should work just like that.

Dylan Leblanc
10-16-2004, 10:33 PM
return;

raf
10-16-2004, 10:38 PM
return;
:confused: Return will stop scriptexecution here since it's used outside a function. When you break out of a loop, you usually want to continue parsing after the loop ...

Ultragames
10-17-2004, 12:52 AM
so break; will take me one layer out right? So if i have an if in a while, than break will put me in teh while. if i have a while, it wil take me out of the while. right?

will return exit the current instance of php? or all? Meaning:

<?
return;
?>
<html>
<?
stuff
?>

raf
10-17-2004, 01:07 AM
break; ends the execution of a loop. So it doesn't make any difference if the break is inside an if-clause or not.

Example


$i = 1;
while ($i < 1000){
$i ++;
if ($i <10){
break; // will end he execution of the while loop
} else {
echo $i;
}
}

will do exactly the same as


$i = 1;
while ($i < 1000){
$i ++;
break; // will end he execution of the while loop
echo $i;
}

some more examples: http://www.php.net/break

I've never seen return; used for that. I've only seen and used that inside function (to stop the function and return the value).
I haven't tested it (because i don't dee any use for it) but i suspect it'll completely stop scriptexecution.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum