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

tripy
—
2008-09-26T09:28:03Z —
#2

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

Jake_Arkinstall
—
2008-09-26T10:05:34Z —
#3

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.

stuffedbuggy
—
2008-09-26T10:18:04Z —
#4

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

Jake_Arkinstall
—
2008-09-26T11:34:00Z —
#5

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.

tripy
—
2008-09-26T11:38:33Z —
#6

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

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?

Jake_Arkinstall
—
2008-09-26T11:54:46Z —
#8

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.

Of course, they are the same, but when considering another variable being modified every iteration, I prefer for()

Jake_Arkinstall
—
2008-09-26T11:57:21Z —
#9

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?

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?

stuffedbuggy
—
2008-09-26T12:21:29Z —
#12

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.

system
—
2009-03-10T15:58:07Z —
#13

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

system
—
2009-03-10T16:10:33Z —
#14

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

system
—
2009-03-10T17:56:58Z —
#15

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