Break for(){}

Discussion in 'PHP' started by G3n3s!s, Mar 3, 2011.

  1. #1
    hey, I need to break for(){} but just for one round, it means something like
    
    for ($i=1;$<51;$i++)
    {
      $q = mysql_fetch_assoc(mysql_query("SELECT * FROM something WHERE postid = (SELECT postid FROM something LIMIT 1)"));
      if ($q['postid'] > 100)
      {
        //do something
      }
      else{
          //do something
          mysql_query("DELETE FROM something WHERE postid = ".$q['postid']);
          /////////////////BREAK HERE, BUT HOW? (because I have deleted it already)
      }
    
      mysql_query("DELETE FROM something WHERE postid = ".$q['postid']);
      //MORE MORE MORE QUERIES
      
    
    PHP:
    I just want one-round break! So for () continues'!
     
    G3n3s!s, Mar 3, 2011 IP
  2. joy1986joy

    joy1986joy Member

    Messages:
    189
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #2
    Use exit(); if you want to break totally....
     
    joy1986joy, Mar 3, 2011 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Use the continue statement:

    
    for ($i=1;$<51;$i++)
    {
        if($i == 1){ continue;}
    
    PHP:
    Basically if i is 1, skip this loop and go to the next condition -> i = 2.
     
    ThePHPMaster, Mar 3, 2011 IP
  4. Mike Griffiths

    Mike Griffiths Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    exit; is totally different from break; and not applicable in this case.

    You need to look at your logic, not a new operator or function. If you only want those if statements to be run the first time round just check the value of $i.
     
    Mike Griffiths, Mar 4, 2011 IP