If you were willing to accept ``` foreach ($foo as $bar) if (is_array) { ... } ```
as a solution, then you might as well use ``` if (is_array($foo)) foreach ($foo as $bar) { ... } ``` I wonder if this could be better achieved by expanding what the error suppression operator `@` can do? This entire behaviour seems more like an error suppression action on `foreach` to me, otherwise should we consider coalescing operators for other types/creating a more generic one? Going back to the error suppression operator: e.g. perhaps ``` foreach ($foo @as $bar) { ... } ``` could prevent skip past execution of the entire foreach block if there is an error using $foo as an array. So might make most sense to place the `@` on `as`, IMO, but I guess arguments could be made to place it like `@foreach ($foo as $bar)` or `foreach @($foo as $bar)`. Regards, Aidan On 11 July 2017 at 20:06, Mark Shust <m...@shust.com> wrote: > Thanks for the great feedback. > > Based on the last mindset on keyword syntax, this comes to mind, intended > to be used similarly to the 'use' keyword when used within the context of a > closure: > > foreach ($foo as $bar) if (is_array) { > ... > } > > > I don't think this is a vast improvement over wrapping this within an > is_array check, however it does avoid the additional nest/wrapping. I was > hoping for something that reads a bit more concisely or with a bit more > syntactical sugar than the above. I think this does read nicely though. > > Cheers, > Mark > > On Tue, Jul 11, 2017 at 1:50 PM Rowan Collins <rowan.coll...@gmail.com> > wrote: > > > On 11 July 2017 16:02:18 BST, Mark Shust <m...@shust.com> wrote: > > >For a syntactic > > >sugar/improvement, this can be shorthand for executing the loop instead > > >of > > >wrapping the block within an is_array check: > > > > > > > > ><?php > > > > > >$foo = "abc"; > > > > > >foreach (??$foo as $bar) { > > > > > > echo $bar; > > > > > >} > > > > Hi! > > > > I think there's definitely the start of a good idea here, but the syntax > > you suggest doesn't read quite right. As has been pointed out, this > differs > > from existing features in two ways: > > > > - the special handling is for any non-iterable value, not just null or > > empty/falsey values, for which you could use $foo??[] and $foo?:[] > > respectively > > - the handling is to skip the loop, not loop once assigning $bar to the > > scalar value, as would happen with (array)$foo > > > > The challenge, then, is to come up with some syntax that somehow suggests > > these rules. The "??" is too much like the null coalesce, which would be > > misleading. > > > > The only idea that immediately comes to mind is a keyword: > > > > foreach ifarray ($foo as $bar) { > > > > I can't say I'm that keen on that syntax, but maybe it will inspire > > someone else. > > > > Regards, > > > > -- > > Rowan Collins > > [IMSoP] > > >