Is something wrong with this code ( for loops in php)


#1

<html>
  <head>
    <title>Solo For Loop!</title>
  </head>
  <body>
    <p>
      <?php
      // Write your for loop below!
      for ($i = 0 ; $i <= 101 ;  $i += 10 ) {
          echo $i;
      }
// or either of this 
for ($i = 0 ; $i  < 101 ; $i = $i + 10 ) {
echo $i;
}

or

for ($i = 0 ; $i <= 101 ; $i = $i + 10) {
echo $i;
}
      
      ?>
    </p>
  </body>
</html>

#2

Do you really have an or stuck in there?

Are you getting any output?
Is the output matching what you had planned?
Are you seeing an error message?
We need a link to the exercise.


#3

https://www.codecademy.com/courses/web-beginner-en-L83Do/0/3?curriculum_id=5124ef4c78d510dd89003eb8

that is the link ..
Here is the instructions:
Write a for loop that counts up by 10, all the way to 100 (e.g. 10, 20, 30...). Inside the for loop, echo the current value of $i, just like in the examples above.

and here is the example as it is stated above

for ($i = 0; $i < 11; $i++) {
echo $i;
}
//either of this

for ($i = 0; $i < 11; $i = $i + 5) {
echo $i;
}


#4

by the way this is not the whole code that I wrote this is just the options on which to choose from



Solo For Loop!



<?php
// Write your for loop below!
for ($i = 0 ; $i <= 101 ; $i += 10 ) {
echo $i;
}
// or either of this
for ($i = 0 ; $i < 101 ; $i = $i + 10 ) {
echo $i;
}

or

for ($i = 0 ; $i <= 101 ; $i = $i + 10) {
echo $i;
}

  ?>
</p>


</html

------------ thats just the options

this is just I wrote



Solo For Loop!



<?php
// Write your for loop below!
for ($i = 0 ; $i < 101 ; $i += 10 ) {
echo $i;
}

  ?>
</p>



#5

Let's try the examples that you've given us.

Example 1:

for ($i = 0 ; $i <= 101 ;  $i += 10 ) {
    echo $i;
}

Gives the following output and passed when I tried it.
0102030405060708090100

Example 2:

for ($i = 0 ; $i  < 101 ; $i = $i + 10 ) {
    echo $i;
}

Gives the following output and passed when I tried it:
0102030405060708090100

Example 3:

for ($i = 0 ; $i <= 101 ; $i = $i + 10) {
    echo $i;
}

Gives the following output and passed when I tried it:
0102030405060708090100

All three for loops that you've shown us meet the requirements of the exercise. They all count up by 10, all the way to 100 (e.g. 10, 20, 30...) and inside the for loop, they echo the current value of $i.

To answer your initial question, no, there is nothing wrong with the code that you've shown us..


#6

thats the problem that specific exercise wont go through because of the bug


#7

No bug. The list is supposed to start with 10 not 0.


#8

I thought about that when I did my own so I did some testing, starting at -100 and the SCT didn't mind.

You haven't told us yet what the error message you are seeing is telling you.


#9

Good to know. I was just going by the instructions.


#10



Solo For Loop!



<?php
// Write your for loop below!
for ($i = 10 ;i < 101 ; $i = $i + 10) {
echo $i;
}
?>



Parse error: syntax error, unexpected ';', expecting ')' on line 10

------- ok this is the code that I wrote


#11

Variable name syntax error... $i


#12

now this is the error:

Oops, try again. Did you set the first part of your loop correctly (the part that starts the loop)? Don't forget your semicolon!

<html>
  <head>
    <title>Solo For Loop!</title>
  </head>
  <body>
    <p>
      <?php
      // Write your for loop below!
      for ($i = 0 ; $i < 101 ;  $i = $i + 10) {
          echo $i;
      }
      
      ?>
    </p>
  </body>
</html>

this is the code


#13

Try refreshing the page and submit again. Your output should look like this...

102030405060708090100

#14

man this never work , so far i have trouble finishing PHP .. and I read alot about bugs in PHP here


#15

Remove the whitespace after the numbers. Your code should look like this:
for ($i = 0; $i < 101; $i = $i + 10) {
echo $i;
}

For some reason this exercise is very picky about the spacing.


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.