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
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) { ... }
Related
I am working on legacy code I have inherited and I have come across this code.
This Code works!
Ignore the variable names look at the { the first Condition opens but never closes the else has no open? it still works?
Is it continuing on both or only one condition and if one which one?
Why does it even work?
foreach($bla as $foo) {
if(condition) {
if(condition) {
// Do Something
}else
continue;
}
}
My assumption is that the inner condition will be using the else? but I am only assuming that because of this answer: Nested if-else behaviour without braces I cannot find a definitive answer for PHP
You may rephrase this code (with proper indentation usage):
foreach($bla as $foo) { // FOREACH
if(condition) { // IF#1
if(condition) { // IF#2
// Do Something
} else continue; // END IF#2
} // END IF#1
} // END FOREACH
Now answers on your questions:
Is it continuing on both or only one condition and if one which one?
It is continueing only if nested if() (IF#2) statement condition evaluated to false.
Why does it even work?
It works because if() allows syntax without brackets for simple operations. See example 1 at the manual. So nested if() closed exactly by continue operation with following delimiter ;.
Yes, the else applies to the inner condition. The code compiles because the else block has no opening brace, so the braces are still balanced. Here's the same code properly indented:
foreach($bla as $foo) {
if(condition) {
if(condition) {
// Do Something
}
else
continue;
}
}
Of course this is inconsistent and a very bad way to write this code.
Surely it means that if the first IF condition is true, it will then go to the second IF condition.
So the second IF condition will only be read if the first one is true.
foreach($bla as $foo) {
if(condition) {
if(condition) {
// Do Something
}else continue;
}
}
If the "else" (or if, for that matter) is a one-liner, it doesn't need braces. The "else" in question is continuing the inner "if".
I have the following PHP code that gives a "Fatal error: Maximum execution time of 30 seconds exceeded" message whenever it runs. If I remove the "if()" statement inside the second for loop the code runs without any problems. I cannot figure out why the "if()" statement would cause this error.
for ($i=1;$i<=$nParam;$i++){
for($j=0;$j<=$nParam-1;$j++){
if ($j=$i-1){
//do something
}
}
}
You are assigning a value, not comparing.
Use:
if ($j === $i-1){
You need to use == for your if statement. The way you have it at the moment is continuously resetting the value of $j to 0, which is making it loop for ever.
Change ($j=$i-1) it to
if ($j == $i-1){
//do something
}
== is for comparison
= is for assignment
hope this help.
You have a typo. You are using the assignment = operator instead of the comparison == operator:
if ($j=$i-1){ // <-- HERE
change it to
if ($j==$i-1){
Please see my code, i am almost stuck in this, Why the break inside the array_walk not breaking...
$bool=array_walk($_POST, 'check_empty');
function check_empty($item, $key)
{
$bool=(isset($item) && $item != "") ? 1: 0 ;
if(!$bool)
{
//return 0;
break;
}
return $bool;
}
It's not at all clear what you're trying to do, but break is a control structure that exits only true loops (for, foreach, while and do-while) and switch structures.
array_walk is iterative but not a loop in the true sense.
You can't 'break' (to use the terminology) from an array walk callback; it is invoked on each element of the array as a means to update or otherwise modify each element, and I can't imagine a use case where you'd want to terminate this during it.
What are you trying to do? Your break is not working, because you're returning a value right before it - so the line with break is never executed.
If you're trying to remove all empty values from the array, try array_filter($array).
I have a situation where when dealing with an object I generally use a foreach to loop through it like this:
foreach ($main_object as $key=>$small_object) {
...
}
However, I need to put a conditional in there like this:
foreach ($main_object as $key=>$small_object) {
if ($small_object->NAME == "whatever") {
// We found what we need, now see if he right time.
if ($small_object->TIME == $sought_time) {
// We have what we need, but how can we exit this foreach loop?
}
}
What is the elegant way to do this? It seems wasteful to have it keep looping through if it's found a match. Or is there another approach to do this that is better? Possibly using for instead of foreach?
From PHP documentation:
break ends execution of the current for, foreach, while, do-while or switch structure.
So yes, you can use it to get out of the foreach loop.
Use the break statement inside the if condition:
if ($small_object->TIME == $sought_time) {
break;
}
break statement will break out of the loop.
$count = 0;
......
..
..
..
..
......
if (++$count == 5) break;
Why this is not possible to accomplish?
foreach($arr as $k => $v)
{
if($condition) { $obj->myMethod() && continue; }
}
After $obj->myMethod() gets evaluated then the keyword continue is evaluated (executed), resulting in skipping the current iteration.
EDIT: i'm asking this because something like:
if($error) { $log->fatal('Something weird happened.') && continue; }
is single line and self-explanatory.
continue is a statement not an expression.
And never the twain shall meet.
You can't put a statement in an expression. (What would echo false && continue; print?)
Instead, use an if, which can contain statements.
You cannot evaluate continue as a condition. The continue keyword does not work the same way as in other languages. In PHP, depending on context continue and break can be somewhat synonymous, consider this construct:
<?php
switch ($months)
{
// start with vowels
case 'august':
break;
case 'april':
continue; // exactly the same as "break" !!!
default:
return 'OK';
}
throw new StartsWithVowelException('Months with vowels are creepy');
?>
While we are on the topic, the break and continue keywords have a feature in PHP that make them a bit more interesting and powerful than their peers in other languages.
Both can be given a numerical argument when used in a loop that indicates how many loops to continue through or break out of. For example, here is an example that restarts the execution of an outer loop from within an inner one::
<?php
//
// verify that each sub array contains the given value
//
$lowerval = strtolower($value);
foreach ($TwoDArray as $otherArray)
{
foreach ($otherArray as $value)
{
if (strtolower($value) == $lowerval)
{
// we found the value -- this one definitely has it.
continue 2;
}
}
// if we've reached here, then the inner loop doesn't have the
// value. ¡aiiee!
}
?>
Hope this helps you out with these 2 constructs, good-luck.
continue is a statement. In PHP a statement and an expression are two different things, statements cannot be evaluated because they do not by nature return true or false which is a requirement for evaluation in PHP.
In PHP you'd have to do something like:
if(test()) continue;