Re: Thoughts on adopting Python's for/foreach else construct.

From: Date: Tue, 25 Feb 2020 20:27:58 +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-108762@lists.php.net to get a copy of this message
On Tue, Feb 25, 2020 at 8:19 PM Michael Morris <tendoaki@gmail.com> wrote: > 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. > See https://wiki.php.net/rfc/loop_else and https://wiki.php.net/rfc/loop_or for previous proposals on the topic. I believe an important concern from previous discussions was that this breaks BC in a really bad way, due to the dangling else conflict. Which is why the latter proposal used "or" instead of "else". Regards, Nikita

« previous php.internals (#108762) next »