-3

Doing some very basic coding on codeacademy and this has been bugging me for over an hour now. What is possibly wrong with this code that it is displaying the error " 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>
2

2 Answers 2

7

The else block must be last. It cannot go before an 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!";
}
1
  • This might be clearer on what is intended : if ($items > 5) { 10% } elseif ($items > 1) { 5% } else { sorry } Commented May 17, 2013 at 23:19
3

Your else block needs to be the last if you intend to use else if. Please watch your use of { and } as well. If it's messy, it's hard to read and harder to debug.

<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>
2
  • now it tells me "Oops, try again! Did you remember to include the elseif keyword?" using the exact code you gave me. EDIT needed to be elseif not else if. I'm pretty sure they were the same.
    – roukzz
    Commented May 17, 2013 at 23:16
  • 1
    Must be some codeacadamy requirement. All I can confirm for you is that this code is valid PHP.
    – Kevin
    Commented May 17, 2013 at 23:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.