Edit report at https://bugs.php.net/bug.php?id=46240&edit=1
ID: 46240 Comment by: paul dot cooperman at gmail dot com 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: So like the try catch in .NET? Previous Comments: ------------------------------------------------------------------------ [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). ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ 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