-3

在 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>
4

2 回答 2

8

else块必须是最后一个。它不能在 a 之前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!";
}
于 2013-05-17T23:07:14.790 回答
4

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

<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>
于 2013-05-17T23:07:42.760 回答