开发者

php loop with countdown

开发者 https://www.devze.com 2022-12-16 15:47 出处:网络
Say i start m开发者_StackOverflow社区y counter at 400. How would i execute a foreach loop that will run backwards until 0?

Say i start m开发者_StackOverflow社区y counter at 400. How would i execute a foreach loop that will run backwards until 0?

pseudocode

$i = 400;
foreach(**SOMETHING**)){
//do stuff
$i--;
}


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
}


In case you really want to iterate backwards over an existing array you can use array_reverse():

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


how about a for loop

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


foreach is used for iterating over sequences or iterators. If you need a conditional loop then use for or while.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号