I've come across a function in some code I'm trying to debug and I'm not sure why it is written like this
foreach ($companies as $company) {
if (!isset($company->account)) {
continue;
}
some_function($company, $sectorsMap, $subSectorsMap);
}
Is this the same?
foreach ($companies as $company) {
if (!isset($company->account)) {
some_function($company, $sectorsMap, $subSectorsMap);
}
}
How does the continue control structure work?
Cheers
If you made the if
statements equivalent, I'd say the top version is more generic and better for more complex statements due to the following:
if - continue
statements, the first approach would allow you to list them in order without nested indentation which could keep your code cleaner. Using the second approach, you could get a lot of nested indentation.if - continue
statement(s), the first approach would have the bulk of the code indented less and possibly easier to read.In your specific example with one statement out side of the continue
, it may not much difference, but if you were going to pick one approach to use everywhere, the first approach might be nicer for complex loops.