Php loop with countdown

Let's say I run my counter at 400. How would I execute a foreach loop that would run back to 0?

pseudo code

$i = 400;
foreach(**SOMETHING**)){
//do stuff
$i--;
}
+3
source share
4 answers
for($i = 400; $i > 0; $i--)
{
  // do stuff
}

other ways to do this:

$i = 400;

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

or

$a = range(400, 1);

foreach($a as $i)
{
  // do stuff
}
+20
source

If you really want to iterate back over an existing array, you can use array_reverse () :

foreach(array_reverse($myArray) as $myArrayElement){
  // do stuff with $myArrayElement
}
+4
source

for

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

foreach . , for while.

+1

Source: https://habr.com/ru/post/1728975/


All Articles