|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61222 For... else construct
Submitted: 2012-03-01 15:27 UTC Modified: 2012-09-22 09:25 UTC
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: jeroenvandenenden at gmail dot com Assigned:
Status: Duplicate Package: Arrays related
PHP Version: Irrelevant OS:
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.
Block user comment
Status: Assign to:
Bug Type:
From: jeroenvandenenden at gmail dot com
New email:
PHP Version: OS:


 [2012-03-01 15:27 UTC] jeroenvandenenden at gmail dot com
It would be nice to have a for...else construct like you see in other languages. 
This makes the code more compact and readable and it avoids the necessity to 
combine an if and a for loop.

Test script:
for ($val in $array) {
  echo $val.'<br />';
} else {
  echo 'No values found';

Expected result:
In the case of an empty array, the else part would be called. Without this 
construct the code would be as follows.

if (count($array) > 0) {
  for ($val in $array) {
    echo $val.'<br />';
} else {
  echo 'No values found';


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-01 15:42 UTC] jeroenvandenenden at gmail dot com
Of course instead of

for ($val in $array) {

I meant

foreach ($array as $val) {
 [2012-03-25 17:57 UTC] phristen at yahoo dot com
There are already bugs for foreach { } else { } and while { } else { }, both highly rated.
I think this should be all consolidated into a single bug or an RFC, or it will never get done.
 [2012-09-22 09:25 UTC]
-Status: Open +Status: Duplicate
 [2012-09-22 09:25 UTC]
Closing as duplicate of
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 25 17:01:27 2024 UTC