|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-07-03 12:06 UTC] w-p at dds dot nl
Description:
------------
In many languages, as in PHP, the is a loop to iterate over all elements of a collection. Many languages also provide a mechanism to prematurely shut this loop down.
That is odd. By using a foreach loop, the programmer communicates that he will use the loop for all the elements. But he doesn't. A common case is looking for an element in a collection in a way that the collection itself does not support. Upon finding the element, the loop is prematurely terminated.
The problem is that the shut-down of the loop is in fact a go-to statement (go to the first statement after the loop) and severely frustrates refactoring. Exit, break and return statements (the hidden go-to's) cannot be safely taken out of a method and be extracted to another one.
It would be better if there was a loop statement that provides the loop the programmer is actually after: a foreach-with-condition loop. This communicates the intention better, and can always be indented properly (how do you indent a hidden go-to statement?)
Off course, there are more uses than looking up something. Like performing actions on only those elements where it makes sense, for example spell-checking on only those elements that have text. In any case, we would have a structure statement that communicates what the loop is doing.
In the example below, I wrote "as long as" in keywords. This is so unlike the rest of PHP that I expect that a symbol would be less out of place. I am thinking of a questing mark or a double question mark.
Test script:
---------------
$objResult = NULL;
$blnFound = FALSE;
foreach($arrElements => $objElement as long as (! $blnFound)):
if($objElement->FullName()=='What I am looking for'):
$objResult = $objElement;
$blnFound = TRUE;
endif;
endforeach;
return $objResult;
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 01:00:02 2025 UTC |
That seems like something you can already easily do with a for-loop (given the array is indexed, else use the array functions to advance the internal pointer): <?php $found = false; for($x = 0, $l = sizeof($elements); $x < $l && !$found; ++$x) { if($elements[$x]->getFullName() == 'Rasmus Lerdorf') { $found = true; } } if($found) { return($elements[$x]); } // ... ??? ... ?>