I have a function like this:
$arrayz = fetch_array_from_somewhere();
if($arrayz) {
$arrayz['foo'] = 12;
}
Now if array ends up empty it will be false, and if it has someting but I don’t care what it has because I am just, for example, adding more data to that array; is it ok to use if() statement this way?
Same question for object arrays (just $arrayz->foo = 12 instead)?
Or should I use empty()?
Edit 1: Just to clarify when I say object array I mean: Array of objects.
So I know that I will always get an array. I don’t care what is in it.
Edit 2:
So in conclusion I should only use this form if I KNOW that I will get an array, either full or array(), either if it is an regular array or an object array (array of objects).
But if I think that I will get something besides that I should use aditional checks.
If
fetch_array_from_somewhereonly return array, then you could do this. An empty array is evaluated asfalse.For object, you can’t do this, object is always evaluated as
trueexcept SimpleXML objects created from empty tags.