Out of curiosity, are the two options below functionally equivalent?
$array_variable = function_that_creates_an_array();
foreach($array_variable as $a){
do_something()
}
vs.
foreach(function_that_creates_an_array() as $a){
do_something()
}
Just want to make sure I'm not calling the function on every iteration or anything dumb like that.
Thanks!
Yes, they are basically equivalent.
The only difference is the first one will add a variable to the current scope (i.e. if your in the global scope).
Simply, yes they are functionally the same.
The two snippet will read the array the same way, without re-evaluation the function.
Nevertheless, in the second snippet, you will not be able to access to the complete array during the loop since you don't have any reference (variable) on it.