Stas,

Wow, for some reason it didn't occur to me to try that. Confirming the
following works:

foreach ($foo ?? [] as $bar) {

  echo $bar;

}


I think the biggest need for this is if the value is null. Guess it's
case-closed :)

Might be a good idea to add this to the docs, pretty neat.


Mark

On Wed, Jul 12, 2017 at 2:00 PM Stanislav Malyshev <smalys...@gmail.com>
wrote:

> Hi!
>
> > Code will error if a non-array value is passed through a looping feature.
> > For example, this code:
> >
> > <?php
> >
> > $foo = "abc";
> >
> > foreach ($foo as $bar) {
>
> Looks like $foo??[] would work here. If $foo is something like false or
> null, that is. If it's not, this is probably a serious bug (why would
> you iterate on something that is a number or a non-iterable object) and
> should be either fixed or resolved explicitly, not hidden.
>
> --
> Stas Malyshev
> smalys...@gmail.com
>

Reply via email to