DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Loop In PHP Using For (Descending)

06.20.2006
| 5965 views |
  • submit to reddit
        For those times you need to do a loop in PHP, but have the outcome be in descending order (ie, start at 10 and end at 1).

$totalcode="10";
for($i=$totalcode; $i>0; $i--){

 echo"$i";
}
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:25pm

Oh, yes! Thanks for that - the less coding the better! :D

Snippets Manager replied on Mon, 2006/07/10 - 11:48am

if you dont mind ending at zero you can also do: $i=10; while ($i--) { echo $i; }