Got a question regarding the following situation in php. Lets say we have 3 files as below:
file1.php:
for ($a=0; $a<=10; $a++) {
require_once("file2.php");
$b = test($a);
echo $b . "\n";
}
file2.php:
function test($val) {
require_once("file3.php"); //does not work
//include("file3.php"); // works
return $array_a[$val];
}
file3.php:
$array_a = array("1" => "A", "2" => "B", "3" => "C", "4" => "D", "0" => "E");
Now when I run file1.php on php cli command line:
The thing that happens is that it will only echo E and after that it errors. Meaning that the file3.php is only included ones in the loop iteration. and on the second loop iteration it goes wrong.
It does require_once the function test() each time iteration, but not the array_a in the second iteration. When I use include instead on the test3.php file it works...
Why is that? the array does not get remembered or included again, but the function test does...
(note that I tried to make a simplistic example, with simple code, just to give the idea)
Thank you