Thoughts on adopting Python's for/foreach else construct.
| From: | Michael Morris | Date: | Tue, 25 Feb 2020 19:18:36 +0000 |
| Subject: | Thoughts on adopting Python's for/foreach else construct. | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-108759@lists.php.net to get a copy of this message | ||
I don't remember if this has been discussed before, but I've been working
with some listing code and wishing for a cleaner way to do this
<?php
if (count($a) === 0) {
// code for no results
}
else {
foreach( $a as $key => $value) {
// code for iteration
}
}
?>
How difficult would it be to make the following work in the interpreter?
<?php
foreach($a as $key => $value) {
// code for iteration
}
else {
// code for no results
}
?>
The code of the else clause executes if the foreach is never entered
regardless of the reason (provided the code didn't outright crash)
Thoughts.