tags:

views:

119

answers:

6
   for(;;)
   {
     if(!$monitor->Throttle($cause))
       die('Fatal error: '.$monitor->error);

     if($cause == THROTTLE_CAUSE_NONE)
       break;

     sleep(60);
   }

i'm a beginner php developer. So how do you read the "for" syntax in previous code. is it valid ?

i got them from http://www.phpclasses.org/blog/post/132-Accelerate-Page-Accesses-Throttling-Background-Tasks-Unusual-Site-Speedup-Techniques-Part-2.html

+13  A: 

for(;;) is a C idiom which means "do forever", an infinite loop. This loop will only exit when either the die statement fires (violently) , or the cause is set to THROTTLE_CAUSE_NONE (not so violently).

It's a for loop with no pre-setup, no condition and not post-iteration commands, effectively the same as while true (pseudo-code).

paxdiablo
+4  A: 

for(;;) is basically an infinite loop, nothing more :)

n3rd
+7  A: 

That's a forever-loop.

joni
+1 for the play on words :P
BoltClock
@BoltCLock I'm happy at least someone recognized it.
joni
+3  A: 

It is valid. It creates an infinite loop, which in this case will be broken out of when/if the break statement executes, i.e. if($cause == THROTTLE_CAUSE_NONE)

William
+4  A: 

Ugh.

It is valid syntax, it creates an infinite loop. But it's ugly.

A much more beautiful way to do this would be

 while ($cause = $monitor->Throttle($cause) != THROTTLE_CAUSE_NONE)
 {
    if(!$cause)
     die('Fatal error: '.$monitor->error);

    sleep(60);   
 }
Pekka
+2  A: 

The for loop has four parts:

for(initialization; exit condition; step) { body; }

Your loop has none of them, so without an exit condition it will just run forever until it hits the 'break' sentence:

if($cause == THROTTLE_CAUSE_NONE)
  break;

An equivalent would be:

while(True) { ... }
Genosh