Re: array coalesce operator concept

From: Date: Wed, 12 Jul 2017 19:10:36 +0000
Subject: Re: array coalesce operator concept
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-99863@lists.php.net to get a copy of this message
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 <smalyshev@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 > smalyshev@gmail.com >

« previous php.internals (#99863) next »