On Wed, 16 Sep 2020 at 04:00, Josh Bruce <j...@joshbruce.dev> wrote:

>
> > * If PHP had either convention or special handling for _ or $_ as a
> “ignore this” destination, I wouldn’t have made the proposal.  However, it
> doesn’t; _ can (apparently!) be a constant, and is also a function, and $_
> has no special handling (and I bet it’s actually used to contain values
> that are used in at least one application).
>
> Saw this today and the list() each() made me think of this thread:
> https://www.dyn-web.com/php/arrays/iterate/
>
> With you on void for same reason and what if the double-arrow was all that
> was needed, like the empty + comma in the link:
>
> foreach ($arr as $key =>)
>

I like this!
Once you get over the perception that something is missing, it is
quite obvious what is going on.

Otherwise, I prefer "foreach ($arr as $key => void)" over "foreach ($arr as
$key => null)".
With "=> null" one might think this is a fancy alternative to
array_filter(). "void" is more explicitly "not a value".

The array_keys() does not work for iterators.

And even on arrays it can be a waste if we are only interested in the first
few keys.
Optimization could mitigate this.
But I would imagine that most people would look at the code and say "that
looks wasteful", because the optimization is not visible or obvious.

If this does not get added to the language, I can live with "=> $_".
PhpStorm already has an option to ignore unused value in foreach(), if a
key exists.
It is not based on the variable name, but this could be added if people ask
for it.

-- Andreas



>
> Cheers,
> Josh

Reply via email to