SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot stuffedbuggy's Avatar
    Join Date
    Sep 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow limit while loop loops?

    I was wondering how can i limit the number of times my while loop will loop?
    You know you cooler than me...

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $cnt=0;
    while(
    true && $cnt<100){
      
    //process....
      
    $cnt++;


  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or, if you like reducing your code:
    PHP Code:
    for($i 0; ($yourcondition) && $i 100$i++){
        
    //process

    Works in the same way, but it removes need to start $i before the loop and increment it at the end.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Zealot stuffedbuggy's Avatar
    Join Date
    Sep 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so i should use a for loop instead of a while loop? am i understanding correct?
    You know you cooler than me...

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's completely upto you.

    I'd prefer the for() because the instructions are all in one command, but the while loop works fine too, and is probably more understandable for beginners.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like to declare my variables before using it. An heritage from my time doing C, I suppose...
    Beside this, the main difference between the 2, is that a for loop is usually used when you know that you will have certain amount of iteration.
    A while loop is more used when you don't know how many iteration are going to be, but that you will need to break the loop at a time X.
    Beside this semantical difference, they are the same.

    You have a difference if you try a do..while loop.
    A while or for loop will test the conditions first, then do the loop.

    A do..while loop will first execute the action, and then test the condition
    PHP Code:

    do{
      if(
    $i_have_found_something===true){
        
    $isFound true;
      }
    } while !isset(
    $isFound); 
    It can be handy sometimes.

  7. #7
    SitePoint Zealot stuffedbuggy's Avatar
    Join Date
    Sep 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in my program, i'm unsure how many times it will loop through, but i don't want it to loop too many times and cause my script to time out. Sounds like limited the while loop is what i need to do. would i implement a counter and the beginning of the loop in the conditional?
    You know you cooler than me...

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I like to declare my variables before using it. An heritage from my time doing C, I suppose...
    PHP Code:
    for($i 
    That's classed as declaration in PHP. In c++ it would be something like:
    PHP Code:
    for(int i 
    is that a for loop is usually used when you know that you will have certain amount of iteration.
    That is it's most common use. However, the for() loop is just a while loop with the condition between a declaration and a repeat instruction, i.e.
    PHP Code:
    <?php
    $i 
    0;
    while(
    condition){
       
    ///do something
       
    $i++;
    }
    //is exactly the same as:
    for($i 0condition$i++){
       
    ///do something
    }
    PHP Code:
    $isFound false;
    do{
      if(
    $i_have_found_something===true){
        
    $isFound true;
      }
    } while (!
    $isFound); 
    is equal to:
    PHP Code:
    for($isFound false$isFound == false; ){
        if(
    $i_have_found_something === false){
            
    $isFound true;
        }

    Of course, they are the same, but when considering another variable being modified every iteration, I prefer for()
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    in my program, i'm unsure how many times it will loop through, but i don't want it to loop too many times and cause my script to time out. Sounds like limited the while loop is what i need to do. would i implement a counter and the beginning of the loop in the conditional?
    PHP Code:
    <?php
    $time 
    microtime();
    $max 1000//1 second
    while($yourothercondition && ((microtime() - time) < $max)){
       
    //yourloop
    }
    or
    PHP Code:
    <?php
    $max 
    1000//1 second
    for($time microtime(); ($yourothercondition && ((microtime() - time) < $max); ){
       
    //yourloop
    }
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Zealot stuffedbuggy's Avatar
    Join Date
    Sep 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a bunch arkinstall
    You know you cooler than me...

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Note that depending on the time it takes to execute each loop, the actual loop running time may be very slightly longer than the time limit you set - because the condition is only to fire the loop, not to end it half-way if that condition is met.

    By the way, if you don't mind saying, what are you doing in this loop?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Zealot stuffedbuggy's Avatar
    Join Date
    Sep 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, i know it seems odd since i seem to have so many questions. Only been doing php part time for a couple of years, but i'm trying to create a crawler to get descriptions for web pages. in my crawler, it loops through the html of a page to find links, and i only want it to retrieve a few. sometimes in may bring back 10 or more, and the script takes 30 seconds or so.
    You know you cooler than me...

  13. #13
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am actually trying to do this.... but when I use

    for($i = 0; ($yourcondition) && $i < 3; $i++){

    //process

    }

    instead of limiting the output to the first 3 items.... it outputs every item 3 times

  14. #14
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $count = 0;
    // my function
    $count++;
    #
    if ($count == 1) break;
    }

    works better....
    but if i change the ($count == 1) to 2, it stops working and outputs all the items again -why is it only working for count==1?

  15. #15
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now i figured it out...... count was reset to 0 each time it looped so i simply movec count=0 to just before the current loop i was working with


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
  •