Req #69988 [Opn->Fbk]: foreach ... as long as ...

From: Date: Thu, 20 Aug 2015 01:19:43 +0000
Subject: Req #69988 [Opn->Fbk]: foreach ... as long as ...
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195360@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69988&edit=1 ID: 69988 Updated by: kalle@php.net Reported by: w-p at dds dot nl Summary: foreach ... as long as ... -Status: Open +Status: Feedback Type: Feature/Change Request Package: *General Issues PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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]); } // ... ??? ... ?> Previous Comments: ------------------------------------------------------------------------ [2015-07-03 12:06:31] 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; ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69988&edit=1

« previous php.bugs (#195360) next »