Re: Draft RFC: foreach iteration of keys without values

From: Date: Thu, 03 Sep 2020 20:08:13 +0000
Subject: Re: Draft RFC: foreach iteration of keys without values
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-111835@lists.php.net to get a copy of this message
On Thu, Sep 3, 2020 at 12:05 PM Sara Golemon <pollita@php.net> wrote: > On Thu, Sep 3, 2020 at 10:35 AM David Rodrigues <david.proweb@gmail.com> > wrote: > > > Do you think that it could be proxied? I mean, optimize foreach > > (array_keys()...) syntax to not call array_keys() in fact, but a > optimized > > version of foreach to handle key only. I don't know it opcache could do > > that, and if it already does. > > > > > I wouldn't use the word "proxied", but yes. In my mind the compiler would > see: > > foreach(\array_keys($arr) as $key) { > > and quietly transform that into: > > foreach ($arr as $key => $_unusedVariableNameThatIsntEvenSpilledToTheScope) > { > > Are there other instances where we have optimizations like this? If so, are they documented? In terms of readability, I'm more likely to do $keys = array_keys($array); foreach($keys as $key){ That would obviously break the optimization we're talking about though. Which makes me wonder if there are other places like that. > Thus not iterating the array twice and creating a temporary array of key > names. > > -Sara > -- Chase Peeler chasepeeler@gmail.com

« previous php.internals (#111835) next »