1

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

vmcie
  • 25
  • 1
  • 4

3 Answers3

0

You are missing quotes with require_once on second function. Also you can just require the file once before the loop instead of including it many times.

require_once("file2.php");
for ($a=0; $a<=10; $a++) {
  $b = test($a);
  echo $b . "\n";
}

require_once("file3.php");
function test($val) {
  //include(file3.php); // works
  return $array_a[$val];
}

NOTE: include does not work, it just does not generate an error since its just an include.

GGio
  • 7,563
  • 11
  • 44
  • 81
  • yes I thought about that and it works like how you described above, but was wondering why it does not work in the example I gave. It does require_once the array_a, but in the second loop iteration it throws out errors. – vmcie Jun 12 '14 at 13:09
0

Just on the docs require_once:

The require_once statement is identical to require except PHP will check if the 
file has already been included, and if so, not include (require) it again.
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • yes correct, but in the second loop iteration it will throw php errors because the array_a seems to be gone or something... – vmcie Jun 12 '14 at 13:08
0

In your 2nd file you are using require_once inside a function. So it is in the scope of that function. In order to use $array_a in 2nd, 3rd, etc iteration, you need to define $array_a as global. So making your 2nd file like this should work:

function test($val) {
    global $array_a;
    require_once("file3.php"); //does not work
    //include("file3.php"); // works
    return $array_a[$val];
}
Dexa
  • 1,641
  • 10
  • 25
  • yes this solves the issue. thank you. Though still looks bit confusing to me why it needs to be global...I was thinking it needs to be only available in that function and does not need to be global...because it should be require_once at each iteration in that function... – vmcie Jun 12 '14 at 13:35
  • Reason is because require_once include file or not based on phisical path of a file, so in a 2nd, 3rd ... iteration it thinks it's already included, but scoping is still applied. [Here](http://stackoverflow.com/a/2679619/2663825) is more detailed answer I just found on SO – Dexa Jun 12 '14 at 13:39
  • The interesting part here is what does the 'global $array_a' statement do? It is not a declarative statement at all. It actually makes an entry in the '$GLOBALS' array. The entry is filled in when File3.php is run. Without the 'global' statement here then the 'array_a' is defined in the function scope and is destroyed when the function exits. PHP 5.3.18 – Ryan Vincent Jun 12 '14 at 13:48
  • thank you. yes this gets to the core of my issue. Still need to think about it for few minutes :) looks bit tricky, atleast to me then :) – vmcie Jun 12 '14 at 13:49
  • @ Ryan Vincent. Yes when the global statement is not used, the example will output only one time E. When global statement is used on the array_a, then the example will output E A B C D – vmcie Jun 12 '14 at 13:53