Re: Thoughts on adopting Python's for/foreach else construct.
| From: | Christian Schneider | Date: | Tue, 25 Feb 2020 19:47:35 +0000 |
| Subject: | Re: Thoughts on adopting Python's for/foreach else construct. | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108760@lists.php.net to get a copy of this message | ||
Am 25.02.2020 um 20:18 schrieb Michael Morris <tendoaki@gmail.com>:
> 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)
I like the idea.
And I think it makes more sense than what Python actually does:
With Python the else clause is executed if the loop is not exited with a break.
https://book.pythontips.com/en/latest/for_-_else.html
So, yes, I would like a foreach .. else but not the Python way ;-)
- Chris