Edit report at https://bugs.php.net/bug.php?id=26411&edit=1
ID: 26411
Updated by: nikic@php.net
Reported by: php at bellytime dot com
Summary: while {} else {}
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: *
PHP Version: *
Block user comment: N
Private report: N
New Comment:
Closing as Wfx as this feature was rejected in https://wiki.php.net/rfc/loop_or.
Previous Comments:
------------------------------------------------------------------------
[2013-02-04 21:34:15] sebi at max-vision dot ro
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.
------------------------------------------------------------------------
[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';
}
?>
------------------------------------------------------------------------
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