I see plenty of code examples where the user is returning from within a foreach loop, but is it bad form to do this? Does it leave any orphaned references, for example?
Here's an example:
$array = array(1, 2, 4, 8, 16);
foreach ($array as $value) {
if ($value == 8) return true;
}
return false;
EDIT: This is quite different from its supposedly duplicate question, simply by the fact that it focuses on the return
instruction, not the break
, continue
, goto
, or any other instruction. Users who are searching for the answer to this question will no necessarily search for the supposed alternatives. In my case, it didn't event occur to me to think them through. Even if I had I would not have come up with an exhaustive list.