php include和require的區別深入解析_PHP教程

來源:互聯網
上載者:User
nclude()
The include() 語句包括並運行指定檔案。

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

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

例子12-3. 基本的 include() 例子
vars.php
複製代碼 代碼如下:
$color = 'green';
$fruit = 'apple';
?>

test.php
複製代碼 代碼如下:
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>

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

例子12-4. 函數中的包括
複製代碼 代碼如下:
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()
複製代碼 代碼如下:
/* 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() 是特殊的語言結構,在條件陳述式中使用必須將其放在語句組中(花括弧中)。

例子12-6. include() 與條件陳述式組
複製代碼 代碼如下:
// 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
複製代碼 代碼如下:
$var = 'PHP';
return $var;
?>

noreturn.php
複製代碼 代碼如下:
$var = 'PHP';
?>

testreturns.php
複製代碼 代碼如下:
$foo = include 'return.php';
echo $foo; // prints 'PHP'
$bar = include 'noreturn.php';
echo $bar; // prints 1
?>

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

http://www.bkjia.com/PHPjc/327653.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327653.htmlTechArticlenclude() The include() 語句包括並運行指定檔案。 以下文檔也適用於require()。這兩種結構除了在如何處理失敗之外完全一樣。include() 產生一個警...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.