當調用腳本第一次啓動或達到include語句時,被包含文件的代碼塊是否'搶佔'?舉個例子:何時包含包含文件?
// execute many lines of code
sleep(10);
// do file retrievals that takes many minutes
include('somefile.php');
如果執行原來的代碼(開始),是把somefile.php的代碼塊存儲在那個瞬間或直到包含語句達到?
當調用腳本第一次啓動或達到include語句時,被包含文件的代碼塊是否'搶佔'?舉個例子:何時包含包含文件?
// execute many lines of code
sleep(10);
// do file retrievals that takes many minutes
include('somefile.php');
如果執行原來的代碼(開始),是把somefile.php的代碼塊存儲在那個瞬間或直到包含語句達到?
當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)
你能指出一個參考嗎? – David
http://php.net/manual/en/function.include.php – 2012-09-24 00:53:30
好的,如果你對我錯了很偏執,那麼爲什麼不製作一個名爲** a.php **的php文件和另一個叫做** ** b.php。'echo'a在這裏';'在** a.php **中有'echo'b在這裏';'在** b.php **中。在echo語句之後的** a.php **中包含** b.php **,看看第一個是什麼,然後,你會得到你的答案。非常簡單。 – 2012-09-24 00:59:08
該文件包含當達到
執行
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
'就像每一個'這是什麼意思?我看不出示例代碼如何證明任何內容。也許如果有時間戳或h:m:s。 – David
您可以使用此代碼測試:
<?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說我認爲是滿意的。
-1這不會回答第1行的問題。不是執行,而是當inc_file.php中的整個代碼塊被「拉入」(因缺少更好的術語)而被執行時。 – David
它有所作爲嗎?包含文件中的代碼只有在達到包含函數時纔會執行,無論文件何時被實際讀入內存。 – iWantSimpleLife
@iWantSimpleLife沒有區別,只是理解水平。 – David
噢,好的。 Php是開源的。您可以獲取源代碼並查看包含如何實現。 ;-) – iWantSimpleLife