Reputation: 159
Not sure if this is the place to ask. Here is my code and my question is after the code:
function test() {
$db->SELECT * FROM... etc...
$array = array("Car"=>$row['car'], "Boat"=>$row['boat'], "Plane"=>$row['plane']);
return $array;
}
$vehicles = test();
echo $vehicles['car']; // call 1
echo $vehicles['boat']; // call 2
echo $vehicles['plan']; // call 3
as you can see, i call $vehicles[];
3 times. Every time it calls the $vehicles
, does it need to go back to the test()
function and search through the database on every call? Or does it store the array in the $vehicles
variable once on page load? thanks.
Upvotes: 0
Views: 72
Reputation: 22911
No. test()
is executed once, and the return variable (Which is an array) is assigned to $vechicles
.
I've created a test here, which shows how this functionality works:
<?php
function test() {
echo 'Test function called', "\n";
$array = array("Car"=> 'Test car', "Boat"=> 'Test boat', "Plane"=> 'Test plane');
return $array;
}
$vehicles = test();
echo $vehicles['Car'], "\n"; // call 1
echo $vehicles['Boat'], "\n"; // call 2
echo $vehicles['Plane'], "\n"; // call 3
You'll notice that "Test function called" is only echoed out once (At the beginning). If this function was called multiple times, "Test function called" would be echoed multiple times.
Upvotes: 2
Reputation: 3178
The function is called once. The call to the $vehicles
variable will pull the content from the array stored, not the function.
So the test()-function is only run once (on page load).
Upvotes: 1