Getting to Know For and ForEach

← Forum
196 pts

Gevin Brown

I receive an error message saying that my loop doesn't echo 20

<?php
for ($i = 0; $i < 11; $i = $i + 10) {
echo $i;
}

The loop is supposed to echo up by 10's and stop at 100.
What am I doing wrong?

  • 459 pts

    psedler

    0 votes

    Did you get any output? Your counter starts at zero, so the first echo would be 0.
    Counter then becomes 10, this is less than 11, so the second echo is 10
    Counter then becomes 20, which is not less than 11, so program stops

    • Gevin Brown 11 months ago
      No, I didn't get any output. I don't understand why the code isn't working, I was told to do this when I looked at the hint. Should I make the variable less than 100?
    • Vera Verhoeven 11 months ago
      for ($leap = 10; $leap <= 100; $leap = $leap+10 ){ echo "<p>$leap</p>"; }
  • 459 pts

    psedler

    0 votes

    First, on the little screen to the right of the coding area -- make that full size to see if you've an answer. If the answer is correct, it is codeacademy's answer checker that is at fault
    Anyway, change $i < 11 to $i < 101. Check the full-size editor screen to see if you've now the correct answer. If so, change echo $i to echo "{$i}"; This format hasn't been talked about, much, but it worked for me

    • psedler 11 months ago
      that should be echo "<p>{$i}</p>";
  • 1256 pts

    Goer Tan

    0 votes

    Vera's code is correct.
    Also, if you are using Google Chrome, try another browser. I used Firefox and the same code on Chrome that did not work, passed on Firefox.

Sign in to post to the forum.

Sign in