This forum is now read-only. Please use our new forums at discuss.codecademy.com.

While Loops in PHP Forum

View Course » View Exercise

1038 points
53f3764e282ae3da53000455_587160470
Submitted by
Julijana Seketa
almost 4 years ago

It passes but shows an error

here is my code but it shows an error saying Parse error: syntax error, unexpected T_ENDWHILE on line 11. I need help! Do I need to add more code to it?

<!DOCTYPE html>
<html>
    <head>
        <title>A loop of your own</title>
        <link type='text/css' rel='stylesheet' href='style.css'/>
    </head>
    <body>
    <?php
    //Add while loop below
    $loopCount = 0;
endwhile ($loopCount<4){
   echo "<p>Iteration number: {$loopCount}</p>";
   $loopCount ++;
}
    ?>
    </body>
</html>

3 votes

permalink

The endwhile loop isn't a different kind of loop, it's just a different syntax for the while loop.
Instead of the opening curly brace, you put a colon and instead of the ending curly brace, you put endwhile followed by a semi-colon. The keyword should still be while.
Here is an example:

while (condition):
    statement
endwhile;

Here is what your code should look like:

<!DOCTYPE html>
<html>
<head>
    <title>A loop of your own</title>
    <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
    <?php
        //Add while loop below
        $loopCount = 0;
        while ($loopCount<4):
            echo "<p>Iteration number: {$loopCount}</p>";
           $loopCount ++;
        endwhile;
    ?>
</body>
</html>

805 points
58e619647cd340ac9f79600c90532a0f?s=140&d=retro
Submitted by
wasabi991011
almost 4 years ago

2 Comments

52557b9580ff33e762003787_90553249 joshcallis6 over 3 years ago

hey, wasabi' I used you're code. It wont work for me?

E6dbbf27e5c54dd7a8270c734d6db866?s=140&d=retro ndavis123 over 2 years ago

I used this exact code and it worked for me