开发者

php loop with countdown

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜