Edit report at https://bugs.php.net/bug.php?id=26411&edit=1
ID: 26411 Comment by: sebi at max-vision dot ro 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: I'd love something like this: if there is at least one iteration, even with break or continue, don't do "else", otherwise, enter the else. Another great would be even better, something like "while(..){..}done{...}else{...}" It could be a new keyword like "whileif". Anyway, even the first proposed structure would be awesome. Previous Comments: ------------------------------------------------------------------------ [2013-02-04 19:56:11] bensor987 at neuf dot fr We shouldn't use "if" or "else" for this. We should use new keywords for this. It seems simple to implement, but maybe they don't want this because of performance concerns ? ------------------------------------------------------------------------ [2013-02-04 09:28:05] kjarli at gmail dot com I don't really see a problem why not to implement this <?php while(!true) { // do something } else[if[...]] { // nothing to do } //Could internally be translated to: $looped = false; while(!true) { $looped = true; // do something } if(!$looped) { // nothing to do } ?> ------------------------------------------------------------------------ [2012-08-23 17:09:14] bensor987 at neuf dot fr I would like to go further in that way : "iterated". "iterated" would be parsed if there has been at least 1 iteration in the loop. <?php ... // Usual Case $array_raw = SomeClass::getAll( $dbconn ); $cnt_raw = count( $array_raw ); $arr_output = array(); for ( $i = 0; $i < $cnt_raw; $i++ ) { $arr_output[] = $array_raw[$i]->id . ' - ' . $array_raw[$i]->label; } if ( $cnt_raw > 0 ){ echo some_function( $arr_output ); } else { echo 'Nothing to display'; } ... ?> <?php ... // Shorter, faster, lighter $array_raw = SomeClass::getAll( $dbconn ); $cnt_raw = count( $array_raw ); $arr_output = array(); for ( $i = 0; $i < $cnt_raw; $i++ ) { $arr_output[] = $array_raw[$i]->id . ' - ' . $array_raw[$i]->label; } iterated { echo some_function( $arr_output ); } none { echo 'Nothing to display'; } ... ?> ------------------------------------------------------------------------ [2012-08-22 17:36:01] bensor987 at neuf dot fr I understand your opinion. You want to make the "else" keyword usable only when "false" is encountered, like in "if...else". I didn't think about this. In that case, let's use another keyword, because this syntax is an excellent idea. Why not this ? <?php while ($row = mysql_fetch_assoc($result)) { print 'Here is a result'; ... } none { print 'No results found'; } ?> ------------------------------------------------------------------------ [2012-08-22 17:05:05] ras...@php.net It's not that simple. Loops are repeatedly evaluated conditions. It isn't clear whether adding an else clause would only apply to the first evaluation or also subsequent ones. ------------------------------------------------------------------------ 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 https://bugs.php.net/bug.php?id=26411 -- Edit this bug report at https://bugs.php.net/bug.php?id=26411&edit=1