Re: Unwrap reference after foreach

From: Date: Sat, 14 Aug 2021 13:21:14 +0000
Subject: Re: Unwrap reference after foreach
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-115718@lists.php.net to get a copy of this message
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 (#115718) next »