Hugh Oxford wrote:
Can I suggest a new PHP native function, "foreachif".
At the moment I am trying to make my code run cleanly even if warnings
are switched on. If I do a foreach on a non-existent array, I get a
warning.
So I have to do
if($array) foreach($array as $foo){};
foreachif would only run if the array was not empty.
You can off course suggest such a function. However, the site of PHP
would probably be a better place.
Apart from that, your workaround tell something really funny: that the
variable $array is a boolean, and if true, it should be iterated upon as
if it were an array.
I KNOW about type juggling, but this is what your code tells me. If you
want to check for an empty array, it would be much clearer to check for
a count being more than zero, or even write a function IsArrayFilled to
check if it is an array at all.
And, off course, there is no problem iterating over an empty array.
There will just be no iteration. Why put an if statement in front of it?
Is this a needless optimization? I think such an optimization is not
necessary, and if it is, it should not surprise me if it was alreadu
built in in the foreach statement.