-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!";
}
Sign up to request clarification or add additional context in comments.

1 Comment

This might be clearer on what is intended : if ($items > 5) { 10% } elseif ($items > 1) { 5% } else { sorry }
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 Comments

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.
Must be some codeacadamy requirement. All I can confirm for you is that this code is valid PHP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.