1. 程式人生 > >[PHP]PHP中include()與require()的區別

[PHP]PHP中include()與require()的區別

今日看php文件,看到include和require區別的講解,感覺這對於初學者是一個很容易迷惑的地方,在此記錄下來,以便自己查閱,也希望對各位php朋友有所幫助。

引用檔案的方法有兩種:require 及 include。兩種方式提供不同的使用彈性。

require 的使用方法如 require("MyRequireFile.php"); 。這個函式通常放在 PHP 程式的最前面,PHP 程式在執行前,就會先讀入 require 所指定引入的檔案,使它變成 PHP 程式網頁的一部份。常用的函式,亦可以這個方法將它引入網頁中。

include 使用方法如 include("MyIncludeFile.php"); 。這個函式一般是放在流程控制的處理部分中。PHP 程式網頁在讀到 include 的檔案時,才將它讀進來。這種方式,可以把程式執行時的流程簡單化。

include()
The include() 語句包括並執行指定檔案。

以下文件也適用於 require()。這兩種結構除了在如何處理失敗之外完全一樣。include() 產生一個警告而 require() 則導致一個致命錯誤。換句話說,如果你想在遇到丟失檔案時停止處理頁面就用 require()。include() 就不是這樣,指令碼會繼續執行。同時也要確認設定了合適的 include_path。

當一個檔案被包括時,其中所包含的程式碼繼承了 include 所在行的變數範圍。從該處開始,呼叫檔案在該行處可用的任何變數在被呼叫的檔案中也都可用。

例子 12-3. 基本的 include() 例子

vars.php
<?php

$color = ’green’;
$fruit = ’apple’;

?>

test.php
<?php

echo \"A $color $fruit\"; // A

include ’vars.php’;

echo \"A $color $fruit\"; // A green apple

?>




如果 include 出現於呼叫檔案中的一個函式裡,則被呼叫的檔案中所包含的所有程式碼將表現得如同它們是在該函式內部定義的一樣。所以它將遵循該函式的變數範圍。

例子 12-4. 函式中的包括

<?php

function foo()
{
global $color;

include ’vars.php’;

echo \"A $color $fruit\";
}

/* vars.php is in the scope of foo() so *
* $fruit is NOT available outside of this *
* scope. $color is because we declared it *
* as global. */

foo(); // A green apple
echo \"A $color $fruit\"; // A green

?>




當一個檔案被包括時,語法解析器在目標檔案的開頭脫離 PHP 模式並進入 HTML 模式,到檔案結尾處恢復。由於此原因,目標檔案中應被當作 PHP 程式碼執行的任何程式碼都必須被包括在有效的 PHP 起始和結束標記之中。

如果“URL fopen wrappers”在 PHP 中被啟用(預設配置),可以用 URL(通過 HTTP)而不是本地檔案來指定要被包括的檔案。如果目標伺服器將目標檔案作為 PHP 程式碼解釋,則可以用適用於 HTTP GET 的 URL 請求字串來向被包括的檔案傳遞變數。嚴格的說這和包括一個檔案並繼承父檔案的變數空間並不是一回事;該指令碼檔案實際上已經在遠端伺服器上運行了,而本地指令碼則包括了其結果。


警告
Windows 版本的 PHP 目前還不支援該函式的遠端檔案訪問,即使 allow_url_fopen 選項已被啟用。


例子 12-5. 通過 HTTP 進行的 include()

<?php

/* This example assumes that www.example.com is configured to parse .php *
* files and not .txt files. Also, ’Works’ here means that the variables *
* $foo and $bar are available within the included file. */

// Won’t work; file.txt wasn’t handled by www.example.com as PHP
include ’http://www.example.com/file.txt?foo=1&bar=2’;

// Won’t work; looks for a file named ’file.php?foo=1&bar=2’ on the
// local filesystem.
include ’file.php?foo=1&bar=2’;

// Works.
include ’http://www.example.com/file.php?foo=1&bar=2’;

$foo = 1;
$bar = 2;
include ’file.txt’; // Works.
include ’file.php’; // Works.

?>


相關資訊參見使用遠端檔案,fopen() 和 file()。

因為 include() 和 require() 是特殊的語言結構,在條件語句中使用必須將其放在語句組中(花括號中)。 [Page]

例子 12-6. include() 與條件語句組

<?php

// This is WRONG and will not work as desired.
if ($condition)
include $file;
else
include $other;


// This is CORRECT.
if ($condition) {
include $file;
} else {
include $other;
}

?>




處理返回值:可以在被包括的檔案中使用 return() 語句來終止該檔案中程式的執行並返回呼叫它的指令碼。同樣也可以從被包括的檔案中返回值。可以像普通函式一樣獲得 include 呼叫的返回值。

注: 在 PHP 3 中,除非是在函式中呼叫否則被包括的檔案中不能出現 return。在此情況下 return() 作用於該函式而不是整個檔案。

例子 12-7. include() 和 return() 語句

return.php
<?php

$var = ’PHP’;

return $var;

?>

noreturn.php
<?php

$var = ’PHP’;

?>

testreturns.php
<?php

$foo = include ’return.php’;

echo $foo; // prints ’PHP’

$bar = include ’noreturn.php’;

echo $bar; // prints 1

?>




$bar 的值為 1 是因為 include 成功運行了。注意以上例子中的區別。第一個在被包括的檔案中用了 return() 而另一個沒有。其它幾種把檔案“包括”到變數的方法是用 fopen(),file() 或者 include() 連同輸出控制函式一起使用。