php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51743 An improvement to control blocks, `else` on loop
Submitted: 2010-05-05 00:09 UTC Modified: 2010-12-01 20:35 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: petepostma at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: ALL
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: petepostma at gmail dot com
New email:
PHP Version: OS:

 

 [2010-05-05 00:09 UTC] petepostma at gmail dot com
Description:
------------
It would be totally kickin' if we could have 'ELSE' on a loop control.
for example, my current code:
-----------------------
$results = mysql_query($sql);
if(!$results || mysql_errno())
  die("gaaah!");

if(mysql_num_rows()<1)
  echo "<tr><td>No results found</td></tr>";
else {
  while($row = mysql_fetch_assoc($result)) {
    //echo rows of a table or something...
  }
}
----------------------
could be:
----------------------
$results = mysql_query($sql);
if(!$results || mysql_errno())
  die("gaaah!");

while($row = mysql_fetch_assoc($result)) {
  //echo rows of a table or something...
} ELSE {
  echo "<tr><td>No results found</td></tr>";
}
----------------------

The definition of an else on a loop would be "If the initial loop condition fails in such a way that the loop never iterates (once or more), then the ELSE block will be executed instead. Loops supported would be while, for, and foreach

This would be great for debugging as well, when you're trying to figure out why things never executed, if there was an 'ELSE { echo " -DEGUB: never entered lool"; } on loops that must be entered it would tip programmer's off right away.

just think about it:
foreach($items as $item) {
   echo "Item: $item\r\n";
} else {
   echo "No items found.\r\n";
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 15:37 UTC] jani@php.net
-Status: Open +Status: Bogus -Package: Unknown/Other Function +Package: Scripting Engine problem
 [2010-12-01 15:37 UTC] jani@php.net
Already requested in bug #46240, please search before spamming the bug database!
 [2010-12-01 20:35 UTC] petepostma at gmail dot com
Sorry, I did not search on foreach loops when I posted. I was specifically looking for while, for, and do. 
Can I at least get a duplicate? After spending the last 9 years using, testing and proofing PHP "Bogus" and "Spam" feels insulting.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 23:01:30 2024 UTC