array coalesce operator concept
| From: | Mark Shust | Date: | Tue, 11 Jul 2017 15:02:18 +0000 |
| Subject: | array coalesce operator concept | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-99834@lists.php.net to get a copy of this message | ||
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