SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with my "for loop"

    Hi everyone

    I have a normal "for loop" below that echo the numbers in the loop. However, i only want the "for loop "to echo out the first 30 numbers, thereafter i want it to echo every 10th number i.e 1, 2, 3, 4 5..... ..... 50, 60, 70 80 90 etc.

    I am not sure how to do it. Can someone please give me advice on how i can do this.


    PHP Code:

    for ($i=1$i <= 150$i++)
    {
        
        echo
    $i <br />";



    Thank you very much everyone.

    Warm regards

    Andreea

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    There are probably many ways of achieving what you want to do, but here is one solution which is arguably easier to read, understand and its openness to change in the future will perhaps more obvious if you have to revisit the code.

    PHP Code:
    $singles range(1,29);
    $tens range(3010010);  // <- 3 args, the last is the 'step'
    $both array_merge($singles$tens);

    foreach(
    $both as $n)
        echo 
    $n PHP_EOL
    It exploits a couple of array functions you might not have been aware of (there are about 80 to choose from).

  3. #3
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Exactly like you stated the problem:

    PHP Code:
    <?php

    for ($i=1$i <= 150$i++) 

        
    // echo out the first 30 numbers
        // if $i equal to or less than 30, echo $i
        
    if($i <= 30) { 
            echo
    $i <br />"

        
    // thereafter i want it to echo every 10th number
        // if $i divisible by 10, echo $i
        
    } elseif($i 10 == 0) { 
            echo
    $i <br />"

        }
    }

    ?>
    Denny Schlesinger
    web services

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    Exactly like you stated the problem:

    PHP Code:
    <?php

    for ($i=1$i <= 150$i++) 

        
    // echo out the first 30 numbers
        // if $i equal to or less than 30, echo $i
        
    if($i <= 30) { 
            echo
    $i <br />"

        
    // thereafter i want it to echo every 10th number
        // if $i divisible by 10, echo $i
        
    } elseif($i 10 == 0) { 
            echo
    $i <br />"

        }
    }

    ?>
    Little bit faster:
    PHP Code:
        } elseif($i >= 30) { 
            echo
    $i <br />"
            
    $i += 9;
        } 
    and remove the = from the IF. It exploits the fact that the iterator of a FOR is mutable inside the loop.

    EDIT: In fact, it'd be faster still if you just made the elseif an else.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You have to learn to walk before you learn to run.
    Denny Schlesinger
    web services


Tags for this Thread

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
  •