Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php loop with countdown

Tags:

foreach

php

Say i start my counter at 400. How would i execute a foreach loop that will run backwards until 0?

pseudocode

$i = 400;
foreach(**SOMETHING**)){
//do stuff
$i--;
}
like image 339
mrpatg Avatar asked Nov 27 '22 06:11

mrpatg


1 Answers

for($i = 400; $i > 0; $i--)
{
  // do stuff
}

other ways to do it:

$i = 400;

while($i > 0)
{
  // do stuff
  $i--;
}

or

$a = range(400, 1);

foreach($a as $i)
{
  // do stuff
}
like image 167
kjagiello Avatar answered Dec 16 '22 01:12

kjagiello