Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Reverse a Loop

  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Reverse a Loop

    I'm currently using the following code as a loop:
    PHP Code:
    for ($counter 0$counter <= $Points$counter += 1
    How many times it loops is based on user input. For example, if the user enters 60 (in the affecting field), it will loop a formula, starting at zero and stopping at sixty. However, I would like to have it start at sixty and go backwards and stop at zero. I've tried playing around with the values in the above code, but I always either [a] get an error, [b] get an infinite loop, or [c] get positive sixty all of the way to negative sixty.

    Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    for ($counter 60$counter >= 0$counter--) 
    Going with your example, is points = 60?
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Technically, my above example will run 61 times. You can change >= to >.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #4
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts
    I've tried that before, but 60 is just an example and is actually determined based on user input. I've tried setting it equal to a variable, but that always results in an error.

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    I realize that 60 is a variable. What I didn't know was the variable name. Thats why I was asking if $points was the variable name. If so, you'd simply replace the 60 in my above code with $points.

    PHP Code:
     <?php
    //this code will run 60 times.
    $points 60;
     for (
    $i=$points$i 0$i--)  {
        echo 
    $i "\n";
     } 
    ?>
    If you're getting an error, it would be helpful to know the exact error you're receiving.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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