php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40218 Add Else clause to while
Submitted: 2007-01-24 14:18 UTC Modified: 2012-09-22 09:27 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: jbailey at raspberryginger dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 5.2.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jbailey at raspberryginger dot com
New email:
PHP Version: OS:

 

 [2007-01-24 14:18 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-24 14:31 UTC] 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 15:08 UTC] 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 ?
 [2012-09-22 09:27 UTC] nikic@php.net
Closing as duplicate of https://bugs.php.net/bug.php?id=26411.
 [2012-09-22 09:27 UTC] nikic@php.net
-Status: Open +Status: Duplicate -Package: Feature/Change Request +Package: *General Issues
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC