example:
foreach($boxes as $box) {
echo "$box \n";
}
Used to be fairly simple, I could just wrap the foreach around a check, for example:
if(is_array($boxes) && count($boxes) > 0) {
}
There is no need to worry that you will receive a warning if for some reason bad input has been passed into the $ boxes array.
When iterators were added to the mix, this no longer works, since Iteratable objects are not arrays. So, I have several solutions, but I am wondering if there is a “best practice” for this.
if($boxes instanceof Traversable && count($boxes) > 0) {
}
if($boxes && count($boxes) > 0) {
}
There are others, but they seem most obvious. Anyone have any suggestions. PHP docs seem quiet.
source
share