是否可以在PHP的for循环中使用三元运算符?

我正在创建一个简单的一页网页,并希望使用for循环来输出导航链接。将阶段编号($x)与字符串配对时出现问题。

最终输出预期为/应为:

  • 利兹,维多利亚桥,第一阶段

  • 第二阶段,第二阶段地点

  • 第三阶段,第三阶段的位置

  • 阶段4,其他

  • 阶段5,其他

但是,输出是

  • 第一阶段,第三阶段的位置

  • 第二阶段,第三阶段的位置

  • 第三阶段,第三阶段的位置

  • 阶段4,其他

  • 阶段5,其他

而且我不太确定自己哪里出了问题。

我尝试使用if / else语句,如下所示。

<!-- vertical nav -->

      <nav class="v-nav p-4">

        <p class="title">Stage</p>

        <ul>

          <?php


          for ($x = 1; $x <= 21; $x++) {


            if ($x = 1) {

              $place = 'Victoria Bridge, Leeds';

            } else if ($x = 2) {

              $place = 'Victoria Bridge 2, Leeds';

            } else {

              $place = 'test'

            }


          ?>

            <li>

              <a href="#stage<?php echo $x ?>" data-number="<?php echo $x ?>">

                <span class="label">Stage <?php echo $x ?> - <?php echo $place ?></span>

                <span class="dot"></span>

              </a>

            </li>

          <?php } ?>

        </ul>

      </nav>

由于$x某种原因,它导致重复21次以上,并阻止了所有其他页面元素的加载。以下是我目前使用三元运算符的内容,似乎无法弄清楚该漏洞在哪里。


<!-- vertical nav -->

      <nav class="v-nav p-4">

        <p class="title">Stage</p>

        <ul>

          <?php for ($x = 1; $x <= 21; $x++) { ?>

            <li>

              <a href="#stage<?php echo $x ?>" data-number="<?php echo $x ?>">

                <span class="label">

                  <?php

                    $navLink = 'Stage '.$x.', '.(

                      $x == 1 ? 'Victoria Bridge, Leeds' :

                      $x == 2 ? 'Second Stage Location' :

                      $x == 3 ? 'Third Stage Location' : 'Any Other');

                    echo $navLink;

                  ?>

                </span>

                <span class="dot"></span>

              </a>

            </li>

          <?php } ?>

        </ul>

      </nav>


繁星点点滴滴
浏览 164回答 2
2回答

开满天机

问题出在您的运营商是否您正在使用=而不是==改用这个:<!-- vertical nav -->&nbsp; &nbsp; &nbsp; <nav class="v-nav p-4">&nbsp; &nbsp; &nbsp; &nbsp; <p class="title">Stage</p>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($x = 1; $x <= 21; $x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($x == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $place = 'Victoria Bridge, Leeds';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($x == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $place = 'Victoria Bridge 2, Leeds';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $place = 'test'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#stage<?php echo $x ?>" data-number="<?php echo $x ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="label">Stage <?php echo $x ?> - <?php echo $place ?></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="dot"></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; </nav>

宝慕林4294392

对于第一个操作员比较,使用错误==而不是=if ($x == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $place = 'Victoria Bridge, Leeds';&nbsp; &nbsp; &nbsp; &nbsp; } else if ($x == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $place = 'Victoria Bridge 2, Leeds';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $place = 'test'&nbsp; &nbsp; &nbsp; &nbsp; }对于三元运算符,请分别使用正确的括号将其打开和关闭Sanbox链接for ($x = 1; $x <= 21; $x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $place&nbsp; = ($x == 1 ?'Victoria Bridge, Leeds' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;($x == 2 ? 'Second Stage Location' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ($x == 3 ? 'Third Stage Location' : 'Any Other')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $place.'<br>';&nbsp; &nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP