Hello,

I wanted to garnish feedback on a RFC proposal. This is just a concept at
this point, and is inspired by the null coalesce operator.

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) {

  echo $bar;

}


will result in the error:

PHP Warning:  Invalid argument supplied for foreach() in test.php on line 3


To prevent this, the logical solution is to wrap this in an is_array check:

<?php

$foo = "abc";

if (is_array($foo)) {

  foreach ($foo as $bar) {

    echo $bar;

  }

}


This code runs successfully and does not error out. 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;

}


Let me know your thoughts.


Cheers,
Mark

Reply via email to