Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PHP for loop to count down instead of up?

Tags:

php

for-loop

I am using PHP to create a dropdown box with years from 1900 to 2012. However, I'd like to have 2012 at the top of the list (i.e. count down from 2012 to 1900 instead of up from 1900 to 2012).

Any help you can provide would be great! :)

<select id="year">                  
<option value="">----</option>
    <?
    for ($i = 1900; $i <= 2012; $i++){
        echo '<option value="'.$i.'">'.$i.'</option>';
    }   
    ?>
</select>
like image 650
Michael Avatar asked Dec 06 '22 17:12

Michael


1 Answers

for($i = 2012; $i >= 1900; $i--)
like image 86
Andreas Wong Avatar answered Dec 10 '22 13:12

Andreas Wong