Re: Draft RFC: foreach iteration of keys without values

From: Date: Thu, 03 Sep 2020 15:11:26 +0000
Subject: Re: Draft RFC: foreach iteration of keys without values
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-111823@lists.php.net to get a copy of this message
On Thu, Sep 3, 2020 at 8:32 AM Sara Golemon <pollita@php.net> wrote: > > On Thu, Sep 3, 2020 at 4:19 AM Markus Fischer <markus@fischer.name> wrote: > > > > I currently use foreach (array_keys($array) as $key) { ... } > > > to avoid complains from code analysers on unused var, is it slower? > > > > one argument brought forward initially (sorry, can't find the email > > right now) is the resource management: array_keys() has to create a copy > > [*] which might be an issue depending on the size of data. > > > > > While I like the idea of more explicit syntax to show intent over a mere > convention of $_ being an ignorable var, I do need to call out the foreach > (array_keys(...) argument as being a poor motivator. > > IF (and I heavily stress "if" here) this pattern is common among people > trying to show explicit intent and IF it represents a noticeable slowdown, > then the solution for it is for the engine to optimize around that by > transforming it during compile time. That lets us fix all usages > instantaneously without user interaction, and more importantly it allows > users to focus on their code being readable (and thereby maintainable) > according to whatever coding standards they choose to apply. > > Again, that same argument is why I actually like the proposal overall. Not > because it's so much more performant, but because it empowers developers to > write code in a way that will be most readable and maintainable to them, > should they happen to just not like the $_ unused var pattern (which is a > legit thing to dislike). > > -Sara Question for those who know about opcache optimizations: is it feasible to avoid fetching the current value if the value is otherwise unused and the variable-variable features are not used either?

« previous php.internals (#111823) next »