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

From: Date: Sun, 19 Dec 2010 12:08:31 +0000
Subject: Req #26411 [Com]: while {} else {}
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-157089@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:         trefighter2334 at aol 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:

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.


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

------------------------------------------------------------------------
[2003-11-25 12:28:30] php at bellytime dot com

Description:
------------
How about a while...else structure. Often we do 

if (!mysql_num_rows($result)) {
   print 'No results found';
}
while ($row = mysql_fetch_assoc($result)) {
   print 'Here is a result';
   ...
}

Wouldn't it be nicer to do a 

while ($row = mysql_fetch_assoc($result)) {
   print 'Here is a result';
   ...
} else {
   print 'No results found';
}




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



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


Thread (28 messages)

« previous php.bugs (#157089) next »