|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-11-25 12:28 UTC] php at bellytime dot com
Description:
------------
How about a while...else structure. Often we do
if (!mysql_num_rows($result)) {
print 'No results found';
}
while ($row = mysql_fetch_assoc($result)) {
print 'Here is a result';
...
}
Wouldn't it be nicer to do a
while ($row = mysql_fetch_assoc($result)) {
print 'Here is a result';
...
} else {
print 'No results found';
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 13:00:02 2025 UTC |
Can you give a better example to support your request? Your example can easily be written as: while ($row = mysql_fetch_assoc($result)) { print 'Here is a result'; ... } if (!row) { print 'No results found'; } I don't think that this one test is so expensive that it makes it worth the trouble (and cost) to clutter up the language.Yes, you're right, my code was wrong. Next try: $found = false; while ($row = mysql_fetch_assoc($result)) { $found = true; print 'Here is a result'; ... } if (!$found) { print 'No results found'; } The proposed while/else syntax is indeed a bit more elegant.try { foreach ($nullValue as $nothing) { echo 'valid input'; } else { //suppress notice echo 'null value'; } //or/and } catch (ExceptionObjectNotIterator $e) { //object doesnt implement Iterator echo $e->getMessage(); }I'd love to see something like Python's while/else logic implemented in PHP. This logical construct would allow developers to define blocks of code in else {} that'd run upon a natural exit from the while(){} loop caused by said loop's condition becoming FALSE; however, the else {} would be skipped over if the loop exits unnaturally -- a la through a break statement or because of an exception. It is to die for on a semi-daily basis for me (in python)... but I'm not sure this is what the developers above (and their examples) have in mind or not. Either implementation would prove useful to me.Highly agreed, this request would be very practical for improved coding structure and follows logical language syntax nicely. function remove_items (&$arr, $needle) { while ($key = array_search($needle, $arr)) { unset( $arr[$key] ); } else return false; // No items present return true; // Item(s) removed }I agree. I think a while () else () will make things easier. I was researching and found out that it doesnt exist yet. while($row = mysqli_fetch_assoc($result)) { } with al else I would fix it.. I am new in PHP,. I will try to find something to fix it. but if the while else is release in a new version that would be nice.Hey, i was also looking for some while {} else {} feature... but i think it could be difficult to implement such a function in php. Because the else{} event would start after the while{}-loop ends -> everytime after the last result! think about it!Why don't you go a little bit further ? while ( $row = mysql_fetch_assoc( $result ) ){ } else for ( $i = 0; $i < $cpt; $i++ ) { } else if ( $dude ) { } else { }I understand your opinion. You want to make the "else" keyword usable only when "false" is encountered, like in "if...else". I didn't think about this. In that case, let's use another keyword, because this syntax is an excellent idea. Why not this ? <?php while ($row = mysql_fetch_assoc($result)) { print 'Here is a result'; ... } none { print 'No results found'; } ?>I would like to go further in that way : "iterated". "iterated" would be parsed if there has been at least 1 iteration in the loop. <?php ... // Usual Case $array_raw = SomeClass::getAll( $dbconn ); $cnt_raw = count( $array_raw ); $arr_output = array(); for ( $i = 0; $i < $cnt_raw; $i++ ) { $arr_output[] = $array_raw[$i]->id . ' - ' . $array_raw[$i]->label; } if ( $cnt_raw > 0 ){ echo some_function( $arr_output ); } else { echo 'Nothing to display'; } ... ?> <?php ... // Shorter, faster, lighter $array_raw = SomeClass::getAll( $dbconn ); $cnt_raw = count( $array_raw ); $arr_output = array(); for ( $i = 0; $i < $cnt_raw; $i++ ) { $arr_output[] = $array_raw[$i]->id . ' - ' . $array_raw[$i]->label; } iterated { echo some_function( $arr_output ); } none { echo 'Nothing to display'; } ... ?>I don't really see a problem why not to implement this <?php while(!true) { // do something } else[if[...]] { // nothing to do } //Could internally be translated to: $looped = false; while(!true) { $looped = true; // do something } if(!$looped) { // nothing to do } ?>I'd love something like this: if there is at least one iteration, even with break or continue, don't do "else", otherwise, enter the else. Another great would be even better, something like "while(..){..}done{...}else{...}" It could be a new keyword like "whileif". Anyway, even the first proposed structure would be awesome.I almost never join conversations or write comments online but I would also love to see this feature as well. It's just nicer, easier to look at and it uses less code which is what I go for. However... if there is a reason behind why you won't do this, I would understand. while ($result = $get->fetch_assoc()) { $in=1; echo 'Something'; } if(!$in){ echo 'Nothing'; } WHEN I could be doing this while ($result = $get->fetch_assoc()) { echo 'Something'; }else{ echo 'Nothing'; }This syntax is causing problems for some because it violates the KISS principles. Since "else" is used for conditions, and since you can write conditions and loops without curly brackets, it could cause confusion to a lot of people. I proposed "iterated" (if at least 1 iteration) and "none" ("else") as replacements, but this request has been rejected shortly after...Plus, consider the following (bad) code : <?php $bool = true; $rand = rand(1, 5); if ($bool) while ($rand != 4) $rand = rand(1, 5); else echo '!bool'; If "else" for loops is implemented, the behaviour of this code will change. I'm sure there are a lot of code like this. That's another reason people don't like this new use for "else".