Edit report at https://bugs.php.net/bug.php?id=46240&edit=1

 ID:                 46240
 Comment by:         amseon at web dot de
 Reported by:        kjarli at gmail dot com
 Summary:            Build in foreach else support
 Status:             Duplicate
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        5.2.6
 Block user comment: N
 Private report:     N

 New Comment:

@[2012-10-17 21:44 UTC] php at yopmail dot com

Thats true, but you could solve it maybe like this:

-------
echo "<ul>";
foreach($data as $item){
    echo "<li>$item</li>";
} else {
    echo "<li>No think to see :(</li>";
}
echo "</ul>";
-------


Previous Comments:
------------------------------------------------------------------------
[2013-02-04 08:58:41] paul dot cooperman at gmail dot com

So like the try catch in .NET?

------------------------------------------------------------------------
[2013-01-04 14:51:50] riccardo dot mastellone at gmail dot com

Having the 'else' in case of an empty array would be soo nice, please do add 
this 
option!

------------------------------------------------------------------------
[2012-10-17 21:44:33] php at yopmail dot com

foreach(){}else{} is a good id but perhaps need to be improved :

this simple example can't be converted to foreachElse (due to <ul/>) :

-------
if(count($data)>0){
    echo "<ul>";
    foreach($data as $item){
        echo "<ul>$item<ul>";
    echo "<ul>";
}
else
{
    echo "<p>No think to see :(</p>";
}
-------

------------------------------------------------------------------------
[2012-09-22 09:30:38] ni...@php.net

Closing as duplicate of https://bugs.php.net/bug.php?id=26411.

------------------------------------------------------------------------
[2011-12-22 20:41:46] jason at valdron dot ca

I completely disagree about the onFail section.

The foreach else would be useful if there is no item. As a shortcut to 
if(count($elements) == 0).

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=46240


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=46240&edit=1

Reply via email to