首頁  >  文章  >  後端開發  >  php中的關於include()使用技巧的詳解

php中的關於include()使用技巧的詳解

黄舟
黄舟原創
2017-06-25 11:05:313889瀏覽


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

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

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

 基本的include() 範例

vars.php
<?php
$color = &#39;green&#39;;
$fruit = &#39;apple&#39;;
?>
test.php
<?php
echo "A $color $fruit"; // A
include &#39;vars.php&#39;;
echo "A $color $fruit"; // A green apple
?>

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

函數中的包含

<?php
function foo()
{
   global $color;
   include &#39;vars.php&#39;;
   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 或其它支援的封裝協定- 所支援的協定見附錄L)而不是本機檔案來指定要被包括的文件。如果目標伺服器將目標檔案作為 PHP 程式碼解釋,則可以用適用於 HTTP GET 的 URL 請求字串#來傳遞變數給被包含的檔案。嚴格的說這和包括一個文件並繼承父文件的變數空間並不是一回事;該腳本文件實際上已經在遠端伺服器上運行了,而本地腳本則包括了其結果。

警告

Windows 版本的 PHP 在 4.3.0 版之前不支援該函數的遠端檔案訪問,即使 allow_url_fopen選項已被啟動。

透過 HTTP 進行的 include()

<?php
/* This example assumes that www.example.com is configured to parse .php *
 * files and not .txt files. Also, &#39;Works&#39; here means that the variables *
 * $foo and $bar are available within the included file.                */
// Won&#39;t work; file.txt wasn&#39;t handled by www.example.com as PHP
include &#39;http://www.example.com/file.txt?foo=1&bar=2&#39;;
// Won&#39;t work; looks for a file named &#39;file.php?foo=1&bar=2&#39; on the
// local filesystem.
include &#39;file.php?foo=1&bar=2&#39;;
// Works.
include &#39;http://www.example.com/file.php?foo=1&bar=2&#39;;
$foo = 1;
$bar = 2;
include &#39;file.txt&#39;;  // Works.
include &#39;file.php&#39;;  // Works.
?>

相關資訊請參閱使用遠端文件,fopen() 和 file()。

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

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() 作用於該函數而不是整個文件。

include() 和 return()語句

return.php
<?php
$var = &#39;PHP&#39;;
return $var;
?>
noreturn.php
<?php
$var = &#39;PHP&#39;;
?>
testreturns.php
<?php
$foo = include &#39;return.php&#39;;
echo $foo; // prints &#39;PHP&#39;
$bar = include &#39;noreturn.php&#39;;
echo $bar; // prints 1
?>


#$bar 的值為 1 是​​因為 include 成功運作了。注意以上例子中的差異。第一個在被包含的文件中用了 return()而另一個沒有。其它幾種把檔案「包括」到變數的方法是用 fopen(),file()或 include() 連同輸出控制函數一起使用。

註: 由於這是一個語言結構而非函數,因此它無法被「變數函數」呼叫。

以上是php中的關於include()使用技巧的詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn