Edit report at https://bugs.php.net/bug.php?id=26411&edit=1

 ID:                 26411
 Comment by:         bensor987 at neuf dot fr
 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 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';
}
...
?>


Previous Comments:
------------------------------------------------------------------------
[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.

------------------------------------------------------------------------
[2012-08-22 16:46:52] bensor987 at neuf dot fr

"no such syntax in other languages"....what do you mean ? PHP doesn't have the 
right innovate ? 

I see plenty of cases I would use this syntax. The "else"s would be triggered 
if there hasn't been any iteration in the loop. It means : less variables to 
use (so less memory usage), less code lines, etc... 

There are only advantages to this syntax (and it's pretty simple to understand, 
no KISS violation here). It's not because it isn't done by other languages that 
we shouldn't implement it.

------------------------------------------------------------------------
[2012-08-22 10:44:30] ni...@php.net

Python has an "else" for loops, but it means something completely different.

------------------------------------------------------------------------
[2012-08-22 10:29:25] hawkyhawk14 at gmail dot com

larue...@php.net
no such syntax in other languages.
---------------------------------

Python has it and some versions of BASIC have it aswell.

------------------------------------------------------------------------


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

Reply via email to