Req #69988 [NEW]: foreach ... as long as ...

From: Date: Fri, 03 Jul 2015 12:06:32 +0000
Subject: Req #69988 [NEW]: foreach ... as long as ...
Groups: php.standards 
Request: Send a blank email to standards-+get-831@lists.php.net to get a copy of this message
From: w-p at dds dot nl Operating system: PHP version: Irrelevant Package: PHP Language Specification Bug Type: Feature/Change Request Bug description:foreach ... as long as ... 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 bug report at https://bugs.php.net/bug.php?id=69988&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69988&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69988&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69988&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=69988&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=69988&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=69988&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=69988&r=needscript Try newer version: https://bugs.php.net/fix.php?id=69988&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=69988&r=support Expected behavior: https://bugs.php.net/fix.php?id=69988&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=69988&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=69988&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=69988&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69988&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=69988&r=dst IIS Stability: https://bugs.php.net/fix.php?id=69988&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=69988&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=69988&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=69988&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=69988&r=mysqlcfg

« previous php.standards (#831) next »