dongpangbu4016 2014-06-24 06:11
浏览 143
已采纳

我希望在php中使用for循环以下输出

I want to display the output as like this

1        1
12      21
123    321
1234  4321
1234554321

Here is my php code

  for($i=1;$i <= 5;$i++)
{
    for($j=1;$j<=$i;$j++)
    {
    // print the result 
     echo "$j";


    }
    for($y=$i;$y<=$i;$y++)
    {
      echo '&nbsp;&nbsp;';
    }
    for($k=$i;$k>=1;$k--)
     {

    // print the result 
      echo "&nbsp;$k";}

    echo "<br/>";
}

But I got the output like this

1   1
12   2 1
123   3 2 1
1234   4 3 2 1
12345   5 4 3 2 1

Please help me get an output like above.

  • 写回答

6条回答 默认 最新

  • dpw50696 2014-06-24 06:21
    关注

    try

    for($i=1;$i <= 5;$i++) {
        for($j=1;$j<=$i;$j++) {
          echo "$j";
        }
        for($y=0;$y<(5-$i)*4;$y++) {
          echo '&nbsp;';
        }
        for($l=$i;$l>0;$l--) {
          echo "$l";
        }
        echo "<br/>";
    }
    

    output:-

    1        1
    12      21
    123    321
    1234  4321
    1234554321
    

    For browser view :- for($y=0;$y<(5-$i)*4;$y++) else correct way to going is for($y=0;$y<(5-$i)*2;$y++)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上