|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72148 foreach-else construct
Submitted: 2016-05-04 07:42 UTC Modified: 2016-05-04 13:26 UTC
From: jdmnst at gmail dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
31 + 5 = ?
Subscribe to this entry?

 [2016-05-04 07:42 UTC] jdmnst at gmail dot com
foreach-else construct, that if foreach-element is empty (or not iterable).

Test script:
$foo = [];

foreach ($foo as $bar) {
   echo $bar;
] else {
   echo 'no bar';

Expected result:
no bar

Actual result:
PHP Parse error:  syntax error, unexpected 'else' (T_ELSE) 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-04 08:10 UTC]
-Package: PHP Language Specification +Package: *General Issues
 [2016-05-04 13:26 UTC]
-Status: Open +Status: Duplicate
 [2016-05-04 13:26 UTC]
Duplicate of bug #46240 for foreach specifically and bug #26411 for loops generically, which is marked as Won't Fix as the RFC has been declined.
 [2016-05-10 13:58 UTC] must at be dot valid
You should really reconsider... it's much more useful than most of the other features you implemented in the past.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Aug 11 20:05:45 2022 UTC