SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do I skip an iteration in a While Loop

    Hi,

    How does one skip an iteration in a Php loop and jump to the next iteration.

    To be exact say I have:

    while ($result_chk_word = mysql_fetch_array($query_chk_word)) {

    $meta_id = $result_chk_word['id'];

    if (in_array($meta_id, $shown_list) {
    //jumpt to next iteration
    }

    do_1();
    do_2();
    do_3();

    }


    Thank you.
    Dean

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,053
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I think you are looking for the keyword continue
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    cpradio ,

    This continue
    does not end the While Loop and will just end the current iteration and jump to the next iteration. Right?
    If right, then that is what I am looking 4.

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,053
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    From the link:
    continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
    So, yes, it does what you want.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Excellent.
    Thanks. And have GR8 day

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    while ($result_chk_word = mysql_fetch_array($query_chk_word)) {

    Safer is...

    Code php:
    while ($result_chk_word = mysql_fetch_array($query_chk_word) !== false) {

    Even better is to move to PDO but that's likely out of scope.

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Safer is...

    Code php:
    while ($result_chk_word = mysql_fetch_array($query_chk_word) !== false) {
    It might be "safer", but will do the wrong thing. $result_chk_word will always be a boolean. You probably meant (note the extra parentheses):

    Code php:
    while (($result_chk_word = mysql_fetch_array($query_chk_word)) !== false) {
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    I did


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
  •