Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    PHP Equivilent to Javas break;

    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:
    PHP Code:
    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?
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    did you try the code you posted? It should work just like that.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    return;

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dylan Leblanc
    Code:
    return;
    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 ...
    Last edited by raf; 10-16-2004 at 10:40 PM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    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:
    PHP Code:
    <?
    return;
    ?>
    <html>
    <?
    stuff
    ?>
    If I'm postin here, I NEED YOUR HELP!!

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    $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
    PHP Code:
    $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.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •