php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69988 foreach ... as long as ...
Submitted: 2015-07-03 12:06 UTC Modified: 2015-08-30 04:22 UTC
From: w-p at dds dot nl Assigned:
Status: No Feedback Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: w-p at dds dot nl
New email:
PHP Version: OS:

 

 [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;



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-03 23:08 UTC] stas@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2015-08-20 01:19 UTC] kalle@php.net
-Status: Open +Status: Feedback
 [2015-08-20 01:19 UTC] kalle@php.net
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]);
}

// ... ??? ...
?>
 [2015-08-30 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 11 14:02:33 2024 UTC