Edit report at https://bugs.php.net/bug.php?id=26411&edit=1
ID: 26411
Comment by: absimu at hotmail dot com
Reported by: php at bellytime dot com
Summary: while {} else {}
Status: Open
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: *
PHP Version: *
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-02-18 01:14:34] ijrbiz at gmail dot com
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
}
------------------------------------------------------------------------
[2010-12-19 13:08:30] trefighter2334 at aol dot com
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.
------------------------------------------------------------------------
[2008-04-09 07:51:11] ois at oisnot dot com
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();
}
------------------------------------------------------------------------
[2003-11-29 08:59:16] [email protected]
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.
------------------------------------------------------------------------
[2003-11-26 17:21:53] [email protected]
Read again. Your code would always print 'No results found' while the intended
change wouldn't.
------------------------------------------------------------------------
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=26411
--
Edit this bug report at https://bugs.php.net/bug.php?id=26411&edit=1