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

For Loops in PHP Forum

View Course

200 points
971718b59902b1eb45c5dade216447c1?s=140&d=retro
Submitted by
Gevin Brown
almost 3 years ago

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?


0 votes

permalink

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

459 points
0d42c68f5516b1ca9d9a76d888053a15?s=140&d=retro
Submitted by
psedler
almost 3 years ago

2 Comments

971718b59902b1eb45c5dade216447c1?s=140&d=retro Gevin Brown almost 3 years 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?

A37c64e0a620b1da2458495f9eee1ca5?s=140&d=retro Vera Verhoeven almost 3 years ago

for ($leap = 10; $leap <= 100; $leap = $leap+10 ){
echo "$leap";
}


0 votes

permalink

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

459 points
0d42c68f5516b1ca9d9a76d888053a15?s=140&d=retro
Submitted by
psedler
almost 3 years ago

1 Comment

0d42c68f5516b1ca9d9a76d888053a15?s=140&d=retro psedler almost 3 years ago

that should be echo "{$i}";


0 votes

permalink

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.

1342 points
9fac5fafbfbd2c2b016a44805acf4a94?s=140&d=retro
Submitted by
Goer Tan
almost 3 years ago