Re: Unwrap reference after foreach

From: Date: Sat, 14 Aug 2021 14:02:04 +0000
Subject: Re: Unwrap reference after foreach
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-115722@lists.php.net to get a copy of this message
Speaking of, i hope that one day we can support javascript-style let in php :) like foreach($it as let &$v){} but that's a discussion for another thread (and i'm sure it has been discussed before, i haven't actually checked though) On Sat, 14 Aug 2021 at 15:23, Hossein Baghayi <hossein.baghayi@gmail.com> wrote: > On Fri, 13 Aug 2021 at 17:59, Nikita Popov <nikita.ppv@gmail.com> wrote: > > > I'd like to address a common footgun when using foreach by reference: > > https://wiki.php.net/rfc/foreach_unwrap_ref > > > > Hello, > I had a question regarding this. > Wouldn't it be possible to limit ``$value``'s scope > to only foreach's > block then discard it? Unless it was already defined elsewhere. > > ``` > foreach($nice_stuff as &$value) {} //we are done here and no need to keep > value around. > > echo $value; // $value is Undefined here. > > $value = null; > foreach($stuff as &$value) {} //we can keep the value here since it doesn't > belong to foreach. > > echo $value; // prints some fancy pancy stuff > ``` >

« previous php.internals (#115722) next »