【问题标题】:Parse error: syntax error, unexpected T_ELSEIF [closed]解析错误:语法错误,意外的 T_ELSEIF [关闭]
【发布时间】:2013-05-13 05:08:10
【问题描述】:

在 codeacademy 上做一些非常基本的编码,这已经困扰了我一个多小时了。 此代码显示错误“Parse error: syntax error, unexpected T_ELSEIF on line 12”可能有什么问题

<html>
  <head>
    <title>Our Shop</title>
  </head>
  <body>
    <p>
      <?php
        $items = 10;    // Set this to a number greater than 5!
        if ($items > 5) {
          echo "You get a 10% discount!"; }
          else { echo "You get a 5% discount!";
          } elseif ($items == 1) {
              echo "Sorry, no discount!";
          }


      ?>
    </p>
  </body>
</html>

【问题讨论】:

标签: php html


【解决方案1】:

else 块必须在最后。它不能在else if 之前:

if ($items > 5) {
    echo "You get a 10% discount!";
} else if ($items == 1) {
    echo "Sorry, no discount!";
} else {
    echo "You get a 5% discount!";
}

【讨论】:

  • 这可能更清楚的目的是什么: if ($items > 5) { 10% } elseif ($items > 1) { 5% } else { sorry }
【解决方案2】:

如果您打算使用else if,您的else 块必须是最后一个块。请注意您对{} 的使用。如果它很乱,就很难阅读,也很难调试。

<html>
  <head>
    <title>Our Shop</title>
  </head>
  <body>
    <p>
      <?php
        $items = 10;    // Set this to a number greater than 5!
        if ($items > 5) {
            echo "You get a 10% discount!";
        } else if ($items == 1) {
            echo "Sorry, no discount!"; 
        } else { 
            echo "You get a 5% discount!";
        }
      ?>
    </p>
  </body>
</html>

【讨论】:

  • 现在它告诉我“糟糕,再试一次!你记得包含 elseif 关键字吗?”使用你给我的确切代码。 EDIT 需要是 elseif 而不是 else if。我很确定它们是一样的。
  • 必须是一些代码学院的要求。我只能为您确认此代码是有效的 PHP。
猜你喜欢
  • 2014-04-02
  • 2011-10-26
  • 2014-09-02
  • 2014-04-26
  • 2013-10-02
  • 2023-04-09
  • 1970-01-01
  • 2013-05-13
  • 2011-09-09
相关资源
最近更新 更多