Req #40218 [Opn->Dup]: Add Else clause to while
| From: | nikic@php.net | Date: | Sat, 22 Sep 2012 09:27:13 +0000 |
| Subject: | Req #40218 [Opn->Dup]: Add Else clause to while | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-173636@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=40218&edit=1
ID: 40218
Updated by: nikic@php.net
Reported by: jbailey at raspberryginger dot com
Summary: Add Else clause to while
-Status: Open
+Status: Duplicate
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Linux
PHP Version: 5.2.0
Block user comment: N
Private report: N
New Comment:
Closing as duplicate of https://bugs.php.net/bug.php?id=26411.
Previous Comments:
------------------------------------------------------------------------
[2007-01-24 15:08:50] didou at keliglia dot com
I like the idea, but using the "else" keyword would break a lot of apps using something
like this:
if (X)
while (foo) {
}
else
no X
whileelse ?
------------------------------------------------------------------------
[2007-01-24 14:31:11] jbailey at raspberryginger dot com
Grr.. This bug tracking system ate my comment. So here's a much shorter version of what I
wrote:
Use cases:
Retrieving from a database (especially since PDO doesn't have a numrows options):
while ($foo = $db->sql_fetchrow($result)) {
// Do something
} else {
// No rows found
}
This could also be useful for ArrayObjects and foreach:
$i = $myArray->getIterator();
foreach ($i as $item) {
// Do something
} else {
// Empty array
}
------------------------------------------------------------------------
[2007-01-24 14:18:57] jbailey at raspberryginger dot com
Description:
------------
Right now checks have to be done separately for when a while will execute zero times, which means
code duplication and constructs that have to be kept in sync. I would like to see an else clause to
while that is executed when the while loop goes zero times.
Reproduce code:
---------------
while (0) {
echo "I ran";
} else {
echo "I didn't run.";
}
Expected result:
----------------
I didn't run.
Actual result:
--------------
Parse error: syntax error, unexpected T_ELSE in /tmp/test.php on line 5
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=40218&edit=1