I downloaded a free newsletter written in php from hotscripts.com
I updated a little the code to add new functionality and I saw something I don’t understand.
In the code I see:
foreach() { ...
if() ...
break;
elseif() ...
continue;
}
I also saw:
function() {
// ...
for($nl = 0; ...
if() ...
return true;
}
I read that break; will stop the loop, continue will skip the loop to the next iteration and return will exit the function.
What I don’t understand is why coding that style? Why not use something like:
function() {
// ...
for($nl = 0; ...
if() ...
$returnValue = true;
else {
$returnValue = false;
}
}
return $returnValue;
}
or same idea in the for loops?
Using keywords such as
breakandcontinuecan make the code far easier to read than what you propose.Especially if you are nesting if/else-statements more than one level.
Compare the snippets further down in this post, which one is easier to read?
Both of them output the same exact thing, and
$Aisarray (1,2,4,4,3,4).A
returnin a loop (inside a function) can save precious CPU cycles, if you know you don’t need to loop any further, why do it?I’m too cool to use
break/continue..I want to have readable code..