php - exclude number in while loop count

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragrid
    New Member
    • Jan 2009
    • 29

    php - exclude number in while loop count

    how can I set variable to a number and loop count from that number to a another with exception/ != certain numbers between variable set number and max loop count number so
    $x = '1'
    while ($x < = 10 ) do something

    but what if I want to skip say 5 , 7 & 8 in code / something that follows
    how can I do this ?
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Either use a foreach loop of an array with the number you do want:
    Code:
    $x = array(0,1,2,5,7,10);
    foreach ($x as $y) {
    // Do something
    }
    Or put in a condition with your while loop:
    Code:
    while ($x<=10) {
    if ($x!=8 && $x!=9) {
    // Do something
    }
    }
    I can't test it right now, but you may even be able to do:
    Code:
    while ($x<=10 && ($x!=8 && $x!=9)) {
    // Do something
    }

    Comment

    Working...