开发者

What is the "for(;;)" syntax in this code?

开发者 https://www.devze.com 2023-01-22 17:44 出处:网络
for(;;) { if(!$monitor->Throttl开发者_高级运维e($cause)) die(\'Fatal error: \'.$monitor->error);
   for(;;)
   {
     if(!$monitor->Throttl开发者_高级运维e($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


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).


That's a forever-loop.


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


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);   
 }


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)


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) { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消