|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-10-06 08:57 UTC] kjarli at gmail dot com
Description:
------------
Each time you want to foreach an array, you first have to check with
a count or empty if you want to give a message or w/e to notify there
is no entry to an array (or object if implements like iterator).
If possible add a else option to foreach.
Reproduce code:
---------------
<?php // old style
if(count($myArray) > 0) {
foreach($myArray as $key => $value) {
}
}
//new style
foreach($myArray as $key => $value) {
} else {
// empty array/object
}
(kinda like how smarty implements it)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 13:00:02 2025 UTC |
Please add a foreach else. It would save so much programming time and eliminate so much room for error. It's so simple... foreach (){} else {} PLEASE PLEASE PLEASE PLEASE PLEASE PLEASETo be honest, I'd like to have a general "onFail{}" handler to put after every function, not only foreach. Like: ---- foreach($arr as $var) { doCode(); } onFail { failHandling(); } ---- or ---- file("file.txt") onFail { failHandling(); } ---- Inside of the onFail-brackets a constant __ERROR__ would be available, with further information on the error. Basically, that onFail-handler would be executed upon receiving the constant FAIL from the function before the onFail-command. RegardsADDITION: 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 readingI 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.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, ... }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.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-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>"; -------