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

andi mitre
STAFF
andi mitre
Treehouse Guest Teacher

Paginating a list: Model and View

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.)

7 Answers

Hello,

If it's for a code challenge, they may want you to code it a specific way, but my code below does the same thing:

$i = 1;
while($i<10) {
   echo $i;
   $i++;
}

This code will display the numbers 1 through 9 on one line without spaces and should be what you are looking for.

Cheers!

andi mitre
STAFF
andi mitre
Treehouse Guest Teacher

So far I have this,

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

... Sorry, I forgot to put a semicolon after the $i++. Make sure you add the semicolon (;) or else the code above won't work.

andi mitre
STAFF
andi mitre
Treehouse Guest Teacher

Thank You! that actually worked just fine.

struck on this CC

<?php $i=0;?> <?php while($i < 10);?> <?php $i=+1;?> <?php echo $i;?> <?php $i++; ?>

please what is the mistake let me know ?

Coders,

Try the way I specified above and it should work. To add, there is no reason to have <?php $i=+1;?> in your code as the rest of your code will do what the code challenge is asking. Also, try to do the whole loop inside one PHP tag instead of multiple ones as this could also help you troubleshoot anything that maybe wrong with your code. I also didn't see any curly braces ( { } ) in your loop as well. Try these things and the code challenge shall allow you to pass.

Cheers!

For the Code Challenge to pass, you need this exact code between the php tags:

$i = 0;
while($i<9) {
  $i++;
  echo $i;
}