开发者

How to skip iterations in a for loop in PHP?

开发者 https://www.devze.com 2023-01-13 20:44 出处:网络
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:

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&l开发者_C百科t;/option>";
  }
?>

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消