Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip iterations in a for loop in PHP?

Tags:

php

for-loop

I have a list of options (booked seats) from which I want to exclude certain values (e.g., 3, 4, 8 and 19). The code I have for constructing the list is:

<?php
for ($i=1; $i<=27; $i++)
  {
    echo "<option value=$i>$i</option>";
  }
?>

How do I exclude 3, 4, 8 and 19 from the list?

like image 204
andesign Avatar asked Aug 30 '10 02:08

andesign


1 Answers

You can use continue to skip the current iteration of a loop.

$exclude = array(3, 4, 8, 19);

for ($i=1; $i<=27; $i++)
{
    if (in_array($i, $exclude)) continue;
    echo "<option value=$i>$i</option>";
}

Documentation.

like image 140
alex Avatar answered Sep 28 '22 02:09

alex