0

I would like to print all the months inside a select tag. Can you point out where I went wrong

 <select>
    <?php
    $array=array("January","February","March","April","May","June","July","August","September","October","November","December");
    for($i=$array[0];$i<=$array[11];$i++)
    {
        echo "<option>$i</option>";
    }
    ?>
</select>

2 Answers2

3

Your loop should be

for ($i=0; $i < count($array); $i++)
{
    echo "<option>$array[$i]</option>";
}
Luffy
  • 1,028
  • 5
  • 13
2

You can use below code too. I have modified your code.

<?php 
$array=array("January","February","March","April","May","June","July","August","September","October","November","December");

echo '<select>';
foreach($array as $month){
  echo "<option>".$month."</option>";
} 
echo '</select>';

?>
Aftab H.
  • 1,517
  • 4
  • 13
  • 25