Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Paginating a List: Model and View Introducing While Loops

li guopeng
li guopeng
8,873 Points

code challenge

Write a while loop to display the numbers 1 through 9, all on one line with no spaces: 123456789. (You will need a counter variable to keep track of the number of times the while loop has executed; name that variable $i.) Bummer! The output should contain only numbers 1 through 9, without any spaces or punctuation.

while.php
<?php
$i= 0;
while ( $i < 9 )
{
  $i+=1;
  echo $i;  
}
?>

Try refreshing the page and entering that again. When I input your code it correctly completes the challenge.

3 Answers

Ok it seems that this is a work-space problem from TeamTreeHouse , forget my answers what i posted and know that your code is right ;)

if you write $i<9 it will only get the 8 not the 9, because the 9 is not <than 9 , with equal it will get also the 9 because the 9 is <= 9

But $i <= 9 will output 1-10, and the quiz is asking for 1-9. If I take their original code and run it through php, I get the output 123456789.

Also, I copied their code directly into the challenge and it passed, so I am pretty sure they hit some sort of Javascript problem with the challenge engine. Please see this screenshot:

https://www.dropbox.com/s/9p5n7w41v4yzfj6/challenge-screen.png?dl=0

$i= 0;
while ( $i <= 9 )
{
  $i+=1;
  echo $i;  
}
?>```
this should work
William Li
William Li
Courses Plus Student 26,868 Points

Hi, Flamur Beqiri can you please also add a little explanation on why your code solves the problem where questioner's code failed? That'd help others understand the problem better.