In the past I suggested operators like
??++
??+=
for this purpose.

But the [? $key] is even better, because it makes it explicit which array
keys we expect to already exist and which we don't.

$var[$k0][? $k1][? $k2]++;

This is great but the second "?" is redundant isn't it?




Marco Pivetta <ocram...@gmail.com> schrieb am Do., 12. Sep. 2019, 16:26:

>  <http://ocramius.github.com/>
>
>
> On Thu, Sep 12, 2019 at 4:02 PM Arvids Godjuks <arvids.godj...@gmail.com>
> wrote:
>
> >
> >
> > чт, 12 сент. 2019 г. в 15:33, Marco Pivetta <ocram...@gmail.com>:
> >
> >> Hey Rowan,
> >> <http://ocramius.github.com/>
> >>
> >>
> >> On Thu, Sep 12, 2019 at 3:30 PM Rowan Tommins <rowan.coll...@gmail.com>
> >> wrote:
> >>
> >> > For instance, for undefined array keys, what if we had an operator for
> >> > "initialise and retrieve", such as $foo[? 'bar']. Then we could
> simplify
> >> > ugly code like this:
> >> >
> >> > if ( ! isset($foo[$key1]) {
> >> >    $foo[$key1] = [];
> >> > }
> >> > if ( ! isset($foo[$key1][$key2]) {
> >> >    $foo[$key1][$key2] = 0;
> >> > }
> >> > $foo[$key1][$key2]++;
> >> >
> >> >
> >> > With something safe but succinct like this:
> >> >
> >> > $foo[? $key1][? $key2]++;
> >> >
> >>
> >> $foo[$key1][$key2] = ($foo[$key1][$key2] ?? 0) + 1;
> >>
> >>
> >>
> > This message contains a healthy dose of sarcasm.
> >
> >
> No sarcasm intended: question is about the verbosity and length of a
> *proper* (correct = no warnings/notices/errors, matches type expectations)
> checked solution, and I provided a simple one-liner that is both readable
> and correct.
>
> You can make what you want out of this, but there was most certainly no
> sarcasm.
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://ocramius.github.com/
>

Reply via email to