0

Is the block of code of an included file 'grabbed' when the calling script first starts or when the include statement is reached? Take for example:

// execute many lines of code
sleep(10);
// do file retrievals that takes many minutes
include('somefile.php');

If the original code is executed (commenced), is the code block of somefile.php put in memory at that instant or not until the include statement is reached?

David
  • 3,285
  • 1
  • 37
  • 54
  • 1
    Does it make a difference? The codes in the included file will only execute when the include functions is reached, regardless of when the file is actually read into memory. – iWantSimpleLife Sep 24 '12 at 00:57
  • @iWantSimpleLife Not a difference, but a level of understanding. – David Sep 24 '12 at 01:04
  • 1
    Oh well. Php is open sourced . You can just get the source and see how include is implemented. ;-) – iWantSimpleLife Sep 24 '12 at 01:42
  • That's over my head for today... Nice answer though (if I knew what to look for). – David Sep 24 '12 at 01:45
  • 1
    @David C ... You need to look at this new Information : http://stackoverflow.com/questions/12617188/derived-class-defined-later-in-the-same-file-does-not-exist/12617996#12617996 – Baba Oct 01 '12 at 15:31

3 Answers3

1

When the include statement is executed/run.

PHP is executed line by line. So, when the process arrives at the include, it will do its magic.

For example:

//some code
//some more code
//even more
include('file.php');//now all of file.php's contents will sit here 
//(so the file will be included at this point)

See http://php.net/manual/en/function.include.php

  • 1
    Okay, well if you're so paranoid about me being wrong, why not make a php file called **a.php** and another called **b.php**. Have `echo'a was here';` in **a.php** and have `echo'b was here';` in **b.php**. Include **b.php** in **a.php** after the echo statement, see what comes first, then, you will have your answer. Quite simple. –  Sep 24 '12 at 00:59
  • I might be missing something. Let's say we start a.php, then modify b.php and save it. Will the modify be included? – David Sep 24 '12 at 00:59
  • You've answered the question by creating some consideration. I remember now the errors 'include file ... not found'. The second half of your answer is about scope, not about the code that exists in the file. Would you consider revising that? – David Sep 24 '12 at 01:30
0

The file is included when the include statement is reached

Execution

a.php

var_dump("a",time());
// execute many lines of code
sleep(10);
// do file retrievals that takes many minutes
include('b.php');

b.php

var_dump("b",time());

Output

string 'a' (length=1)
int 1348447840
string 'b' (length=1)
int 1348447850
Baba
  • 94,024
  • 28
  • 166
  • 217
  • 'like every exactly' What does that mean? I don't see how the example code proves anything. Maybe if there was a timestamp or h:m:s. – David Sep 24 '12 at 00:54
-1

You can test it using this code:

<?php

echo 'Before sleep(): ' . $test . ' | ';

sleep(10);

echo 'After sleep(): ' . $test . ' | ';

include('inc_file.php');

echo 'After include(): ' . $test;

?>

Assuming that inc_file.php has this code:

<?php

$test = 'Started var';

?>

The output will be:

Before sleep(): | After sleep(): | After include(): Started var

So we can say that inc_file.php content will be available only after include() be called.

I didn't find a clear explanation in PHP docs, but what @navnav said i think is satisfatory.

Marcio Mazzucato
  • 8,841
  • 9
  • 64
  • 79
  • -1 This doesn't answer the question on line 1. Not execution, but when the entire block of code in inc_file.php is 'pulled in' (for lack of a better term) to be executed. – David Sep 24 '12 at 02:27