Limit while loop loops?

I was wondering how can i limit the number of times my while loop will loop?


$cnt=0;
while(true && $cnt<100){
  //process....
  $cnt++;
}

Or, if you like reducing your 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.

so i should use a for loop instead of a while loop? am i understanding correct?

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.

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



do{
  if($i_have_found_something===true){
    $isFound = true;
  }
} while !isset($isFound);

It can be handy sometimes.

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?

I like to declare my variables before using it. An heritage from my time doing C, I suppose…

for($i = 0

That’s classed as declaration in PHP. In c++ it would be something like:

for(int i = 0

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
$i = 0;
while(condition){
   ///do something
   $i++;
}
//is exactly the same as:
for($i = 0; condition; $i++){
   ///do something
}

$isFound = false;
do{
  if($i_have_found_something===true){
    $isFound = true;
  }
} while (!$isFound);

is equal to:

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() :slight_smile:

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
$time = microtime();
$max = 1000; //1 second
while($yourothercondition && ((microtime() - time) < $max)){
   //yourloop
}

or

<?php
$max = 1000; //1 second
for($time = microtime(); ($yourothercondition && ((microtime() - time) < $max); ){
   //yourloop
}

thanks a bunch arkinstall

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?

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.

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

$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?

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