2012-09-24 142 views
-1

当调用脚本第一次启动或达到include语句时,被包含文件的代码块是否'抢占'?举个例子:何时包含包含文件?

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

如果执行原来的代码(开始),是把somefile.php的代码块存储在那个瞬间或直到包含语句达到?

+1

它有所作为吗?包含文件中的代码只有在达到包含函数时才会执行,无论文件何时被实际读入内存。 – iWantSimpleLife

+0

@iWantSimpleLife没有区别,只是理解水平。 – David

+1

噢,好的。 Php是开源的。您可以获取源代码并查看包含如何实现。 ;-) – iWantSimpleLife

回答

1

当include语句被执行/运行时。

PHP是逐行执行的。所以,当程序到达include时,它会发挥它的魔力。

例如:

//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) 

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

+0

你能指出一个参考吗? – David

+1

http://php.net/manual/en/function.include.php – 2012-09-24 00:53:30

+1

好的,如果你对我错了很偏执,那么为什么不制作一个名为** a.php **的php文件和另一个叫做** ** b.php。'echo'a在这里';'在** a.php **中有'echo'b在这里';'在** b.php **中。在echo语句之后的** a.php **中包含** b.php **,看看第一个是什么,然后,你会得到你的答案。非常简单。 – 2012-09-24 00:59:08

0

该文件包含当达到

执行

a.php只会

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

b.php

var_dump("b",time()); 

输出

string 'a' (length=1) 
int 1348447840 
string 'b' (length=1) 
int 1348447850 
+0

'就像每一个'这是什么意思?我看不出示例代码如何证明任何内容。也许如果有时间戳或h:m:s。 – David

-1

您可以使用此代码测试:

<?php 

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

sleep(10); 

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

include('inc_file.php'); 

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

?> 

假设inc_file.php有这样的代码:

<?php 

$test = 'Started var'; 

?> 

输出结果为:

睡前():|睡眠后():| include()之后:已启动var

所以我们可以说inc_file.php只有在调用include()后才能使用内容。

我没有在PHP文档中找到明确的解释,但@navnav说我认为是满意的。

+0

-1这不会回答第1行的问题。不是执行,而是当inc_file.php中的整个代码块被“拉入”(因缺少更好的术语)而被执行时。 – David