SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;
         } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  4. #4
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);
         } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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