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

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

 New Comment:

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).


Previous Comments:
------------------------------------------------------------------------
[2011-07-12 12:13:44] dinumarina at yahoo dot com

Opposing general consensus, I think adding such feature would be semantically 
ambiguous. The foreach function does not fail, it has types it can handle 
(assoc 
array, object) and that it can't handle (scalar). A function can always return 
a 
result that may be valid for foreach but not the expected format, so I think 
data sanitizing is best done dilligently. This is just a lazy-man's hack for a 
non-issue. For the function completion status (onFail), there is already such a 
construct: try {throw()} catch(){} which already supersets the imagined onFail 
implementation. Only thing I would hindsight: it would have been SOOO nice if 
php had a false/null/na result convention, as well as an error/exception 
convention and it would actually stick by it. Each module signals completion, 
computability, singular cases and errors as it well pleases.

------------------------------------------------------------------------
[2011-02-18 01:20:24] ijrbiz at gmail dot com

Highly agreed with adding a foreach :: else statement, this request would be 
very 
practical for improved coding structure and follows logical language syntax 
nicely.

foreach ($items as $item) {
        echo $item; // Manage each item, ...
} else {
        echo 'No items present.'; // Manage no items found, ...
}

------------------------------------------------------------------------
[2011-02-04 02:30:00] pedro at worcel dot com

foreachelse seems ok to me. Onfail is... weird. :)

------------------------------------------------------------------------
[2010-12-20 13:40:54] rick dot sketchy at gmail dot com

I have to agree with the OP. foreachelse (or something similar) is really 
needed. I do like the suggestion posted by cerlestes at googlemail dot com:

foreach($arr as $var)
{
    doCode();
}
onFail
{
    failHandling();
}

A fail handler would prove useful,however I can see it may have some 
limitations, in which case an else option on the foreach would be satisfactory. 
Whilst we're at it, it may as well be added to while statements too.

------------------------------------------------------------------------
[2010-02-13 19:44:15] cerlestes at googlemail dot com

ADDITION:

Why I would like to have this is because of the following situation:

----
$test = (float)0; // This would be the return of a function.

// Failhandling
if(!$test)
{
    doFailhandling();
}else ...
----

This method has room for misinterpretations.
Ofc, you could test for "$test === false", but I think a general onFail-handler 
would be way nicer.

Thanks for reading

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


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