Results 1 to 6 of 6

Thread: How to use Break and Continue in PHP?

  1. #1
    Join Date
    Jul 2006
    Posts
    339

    How to use Break and Continue in PHP?

    Hi friends,
    I have just started doing the PHP programming language. I am confused with some constructs of the control structure. I want to know more about the break and continue. I have used the break statement in C program but I don't know how to use it in PHP.!! So please tell me how to use Break and Continue in PHP? Any other information would be most welcomed.!!

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: How to use Break and Continue in PHP?

    The break statement exits a structure for, foreach, while, do-while or switch. Before using the break or the continue, you should have a good knowledge about the constructs and the loops that are used in PHP. The break accepts an optional numeric argument which tells it how many nested structures have been disrupted. The continue and break are used within PHP loops to skip iterations and exit the loops respectively. To make it very clear, if you use "break" with no numerical argument, that's the same as doing "break 1". "break 0", while allowed, does nothing.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Jul 2006
    Posts
    289

    Re: How to use Break and Continue in PHP?

    The continue statement is used in a loop to avoid the instructions of the current iteration and continue execution at the condition evaluation and then to begin the next iteration. You should keep in mind that in PHP, structure switch is considered a loop continues. 'continue' accepts an optional numeric argument which tells it how many nested structures were ignored. 0 continues, and continues 1, are identical to continuous. The switch statement is equivalent to a series of if statements. On many occasions you will need to compare the same variable or an expressions with many different values, and execute different parts of code depending on which value it equals.
    Signatures reduce available bandwidth

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to use Break and Continue in PHP?

    I have provided you with an example in which I have used a break statement. Just have a look, so that you can get an idea how to use it :
    PHP Code:
    <?php
    $arr 
    = array('one''two''three''four''stop''five');
    while (list(, 
    $val) = each($arr)) {
        if (
    $val == 'stop') {
            break;
        }
        echo 
    "$val<br />\n";
    }
     
    $i 0;
    while (++
    $i) {
        switch (
    $i) {
        case 
    6:
            echo 
    "At 5<br />\n";
            break 
    1
        case 
    10:
            echo 
    "At 10; quitting<br />\n";
            break 
    2;
        default:
            break;
        }
    }
    ?>

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: How to use Break and Continue in PHP?

    In the following example provided by me, you will understand how to use a continue in coding :
    PHP Code:
    <?php
    while (list($key$value) = each($arr)) {
        if (!(
    $key 2)) { 
            continue;
        }
        
    do_something_odd($value);
    }
     
    $i 0;
    while (
    $i++ < 5) {
        echo 
    "Outer<br />\n";
        while (
    1) {
            echo 
    "&nbsp;&nbsp;Middle<br />\n";
            while (
    1) {
                echo 
    "&nbsp;&nbsp;Inner<br />\n";
                continue 
    3;
            }
            echo 
    "This never gets output.<br />\n";
        }
        echo 
    "Neither does this.<br />\n";
    }
    ?>

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use Break and Continue in PHP?

    The following is an example regarding the condition at the end of the loop and continue :
    PHP Code:
    <?php 
    $i 
    0;
    do {
        
    printf('%d '$i);
        continue;
    } while( ++
    $i 10);
    ?>
    The output of the above example would be :
    Code:
    Output: 0 1 2 3 4 5 6 7 8 9
    It gets executed all the time regardless that continue is placed before the while() statement. That does not get skipped.

Similar Threads

  1. Replies: 2
    Last Post: 17-02-2012, 02:09 PM
  2. Replies: 5
    Last Post: 14-12-2011, 06:09 PM
  3. Is it possible to break into wii 4.3
    By Ryder Allen in forum Portable Devices
    Replies: 5
    Last Post: 04-03-2011, 10:06 AM
  4. Is it possible to break AES easily
    By aahisTA in forum Networking & Security
    Replies: 3
    Last Post: 23-12-2010, 07:37 AM
  5. How to use Break and Continue Statements in JavaScripts?
    By warehouse peon in forum Software Development
    Replies: 5
    Last Post: 30-01-2010, 06:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,567,887.94449 seconds with 17 queries