doter1995 2017-04-23 11:26
浏览 40
已采纳

使用三元和冒号进行循环

This is from a tutorial for php, I cant understand the use of the ternary ? And the use of the colon : Can you please explain to me the use of the colon in here thanks I tried to read the tutorial and php reference but couldn't understand it

This code will print a side way pyramid

 for ($row = 1; $row <= 5; $row++)
  {
      for ($col = 1; $col <= ($row > 3 ? 6 - $row : $row); $col++)
      {
          echo '*';
      }

      echo "<br>";
  }
  • 写回答

2条回答 默认 最新

  • dongwei1895 2017-04-23 11:29
    关注

    This is the same as the following code:

    for ($row = 1; $row <= 5; $row++)
    {
        if ($row > 3)
            $max = 6 - $%row;
        else 
            $max = $row;
    
        for ($col = 1; $col <= $max; $col++)
        {
            echo '*';
        }
    
        echo "<br>";
    }
    

    The colon is part of the ternary operator:

    A ? B : C
    

    equals to

    if (A) then B else C
    

    For more information please check the documentation on the ternary (?:) operator.

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

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化