# Thread: help with for loops

1. ## help with for loops

I right now wish to have a for loop that will give me numbers

0.1 through 9.9 each time adding 0.1 to it. However, I can't for the life of me figure out how to get PHP to let me do this. Seems I can't figure out how doubles are supposed to work here. Please help!

2. That one is a little tricky and I couldn't get it to work with a tradional for() loop, but I did accomplish it using a while() loop at printf()

Check out the formatting options here same for printf()
http://www.php.net/sprintf

PHP Code:
\$i 0.1;
while(
\$i 9.9) {

printf('%1.1f<br>'\$i);

\$i += 0.1;
}

3. Very simple... Instead of your normal increment op (\$i++) replace it with anything...

PHP Code:

for(\$i=0.1\$i <= 9.9\$i \$i 0.1)[

//Loopy stuff...

C'est tres facile...

The third arg in the loop comand is a line of code that is run at the end of each loop and checked with the first arg to see if it goes for another go.

4. Thanks freddy, you'rs worked great (after I tweaked it for my purposes )

Milamber

Originally posted by freddydoesphp
That one is a little tricky and I couldn't get it to work with a tradional for() loop, but I did accomplish it using a while() loop at printf()

Check out the formatting options here same for printf()
http://www.php.net/sprintf

PHP Code:
\$i 0.1;
while(
\$i 9.9) {

printf('%1.1f<br>'\$i);

\$i += 0.1;
}

5. New Oddity, you are correct, and I don't know what I was thinking, you can do it with a for() loop

PHP Code:
for(\$i=0.1;\$i<=9.9;\$i+=0.1) {

printf('%1.1f<br>'\$i);
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•