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]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to