Req #46240 [Com]: Build in foreach else support

From: Date: Fri, 18 Feb 2011 00:20:24 +0000
Subject: Req #46240 [Com]: Build in foreach else support
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-158846@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=46240&edit=1 ID: 46240 Comment by: ijrbiz at gmail dot com Reported by: kjarli at gmail dot com Summary: Build in foreach else support Status: Open Type: Feature/Change Request Package: Scripting Engine problem Operating System: * PHP Version: 5.2.6 Block user comment: N Private report: N New Comment: Highly agreed with adding a foreach :: else statement, this request would be very practical for improved coding structure and follows logical language syntax nicely. foreach ($items as $item) { echo $item; // Manage each item, ... } else { echo 'No items present.'; // Manage no items found, ... } Previous Comments: ------------------------------------------------------------------------ [2011-02-04 02:30:00] pedro at worcel dot com foreachelse seems ok to me. Onfail is... weird. :) ------------------------------------------------------------------------ [2010-12-20 13:40:54] rick dot sketchy at gmail dot com I have to agree with the OP. foreachelse (or something similar) is really needed. I do like the suggestion posted by cerlestes at googlemail dot com: foreach($arr as $var) { doCode(); } onFail { failHandling(); } A fail handler would prove useful,however I can see it may have some limitations, in which case an else option on the foreach would be satisfactory. Whilst we're at it, it may as well be added to while statements too. ------------------------------------------------------------------------ [2010-02-13 19:44:15] cerlestes at googlemail dot com ADDITION: Why I would like to have this is because of the following situation: ---- $test = (float)0; // This would be the return of a function. // Failhandling if(!$test) { doFailhandling(); }else ... ---- This method has room for misinterpretations. Ofc, you could test for "$test === false", but I think a general onFail-handler would be way nicer. Thanks for reading ------------------------------------------------------------------------ [2010-02-13 19:38:00] cerlestes at googlemail dot com To be honest, I'd like to have a general "onFail{}" handler to put after every function, not only foreach. Like: ---- foreach($arr as $var) { doCode(); } onFail { failHandling(); } ---- or ---- file("file.txt") onFail { failHandling(); } ---- Inside of the onFail-brackets a constant __ERROR__ would be available, with further information on the error. Basically, that onFail-handler would be executed upon receiving the constant FAIL from the function before the onFail-command. Regards ------------------------------------------------------------------------ [2009-10-06 08:37:09] ptmcnally at gmail dot com I agree, this would be a nice addition to PHP6. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/bug.php?id=46240 -- Edit this bug report at http://bugs.php.net/bug.php?id=46240&edit=1

« previous php.bugs (#158846) next »