开发者

Another php nested loop

开发者 https://www.devze.com 2023-02-20 23:46 出处:网络
How can I print this output using a FOR - NESTED loop using PHP? It\'s not really helpful for me getting ready code, I want to understand how to reach this result using 2 for loops.

How can I print this output using a FOR - NESTED loop using PHP?

It's not really helpful for me getting ready code, I want to understand how to reach this result using 2 for loops.

5
5开发者_开发技巧4
543
5432
54321


for($i=5; $i>=1; $i--)
{
  for($j=5; $j>=$i; $j--)
  {
    echo $j;
  }
}


Well a general way to solve loop problems is writing your code linear at first, without loops

So, write 5 loops first, each printing one line.
Then try to find some regularity in them and combine in one


With one for loop. I don't see any reason to have two loops. If there is a reason beyond this being an assignment then please explain that reason in your question.

$str = '';
for($i = 5; $i > 0; $i--) {
   $str .= $i;
   echo $str . "\n";
}
0

精彩评论

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

关注公众号