#46240 [Com]: Build in foreach else support

From: Date: Sat, 13 Feb 2010 19:44:15 +0000
Subject: #46240 [Com]: Build in foreach else support
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-147575@lists.php.net to get a copy of this message
ID: 46240 Comment by: cerlestes at googlemail dot com Reported By: kjarli at gmail dot com Status: Open Bug Type: Feature/Change Request Operating System: * PHP Version: 5.2.6 New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ [2009-09-07 01:13:01] john at brahy dot com Please add a foreach else. It would save so much programming time and eliminate so much room for error. It's so simple... foreach (){} else {} PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE ------------------------------------------------------------------------ [2008-10-06 08:57:14] kjarli at gmail dot com Description: ------------ Each time you want to foreach an array, you first have to check with a count or empty if you want to give a message or w/e to notify there is no entry to an array (or object if implements like iterator). If possible add a else option to foreach. Reproduce code: --------------- <?php // old style if(count($myArray) > 0) { foreach($myArray as $key => $value) { } } //new style foreach($myArray as $key => $value) { } else { // empty array/object } (kinda like how smarty implements it) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46240&edit=1

« previous php.bugs (#147575) next »