Req #26411 [Com]: while {} else {}

From: Date: Fri, 18 Feb 2011 00:14:35 +0000
Subject: Req #26411 [Com]: while {} else {}
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-158845@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=26411&edit=1 ID: 26411 Comment by: ijrbiz at gmail dot com Reported by: php at bellytime dot com Summary: while {} else {} Status: Open Type: Feature/Change Request Package: Scripting Engine problem Operating System: * PHP Version: * Block user comment: N Private report: N New Comment: Highly agreed, this request would be very practical for improved coding structure and follows logical language syntax nicely. function remove_items (&$arr, $needle) { while ($key = array_search($needle, $arr)) { unset( $arr[$key] ); } else return false; // No items present return true; // Item(s) removed } Previous Comments: ------------------------------------------------------------------------ [2010-12-19 13:08:30] trefighter2334 at aol dot com I'd love to see something like Python's while/else logic implemented in PHP. This logical construct would allow developers to define blocks of code in else {} that'd run upon a natural exit from the while(){} loop caused by said loop's condition becoming FALSE; however, the else {} would be skipped over if the loop exits unnaturally -- a la through a break statement or because of an exception. It is to die for on a semi-daily basis for me (in python)... but I'm not sure this is what the developers above (and their examples) have in mind or not. Either implementation would prove useful to me. ------------------------------------------------------------------------ [2008-04-09 07:51:11] ois at oisnot dot com try { foreach ($nullValue as $nothing) { echo 'valid input'; } else { //suppress notice echo 'null value'; } //or/and } catch (ExceptionObjectNotIterator $e) { //object doesnt implement Iterator echo $e->getMessage(); } ------------------------------------------------------------------------ [2003-11-29 08:59:16] elmicha@php.net Yes, you're right, my code was wrong. Next try: $found = false; while ($row = mysql_fetch_assoc($result)) { $found = true; print 'Here is a result'; ... } if (!$found) { print 'No results found'; } The proposed while/else syntax is indeed a bit more elegant. ------------------------------------------------------------------------ [2003-11-26 17:21:53] helly@php.net Read again. Your code would always print 'No results found' while the intended change wouldn't. ------------------------------------------------------------------------ [2003-11-25 19:16:51] elmicha@php.net Can you give a better example to support your request? Your example can easily be written as: while ($row = mysql_fetch_assoc($result)) { print 'Here is a result'; ... } if (!row) { print 'No results found'; } I don't think that this one test is so expensive that it makes it worth the trouble (and cost) to clutter up the language. ------------------------------------------------------------------------ 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=26411 -- Edit this bug report at http://bugs.php.net/bug.php?id=26411&edit=1

« previous php.bugs (#158845) next »