本文目錄一覽:
php 中include怎麼用
使用方法:include “文件路徑”;
函數作用:引入另一個php腳本文件,並執行裡面的代碼
推薦使用:include_once “文件路徑”;
php include()的具體用法
nclude語句會獲取指定文件中存在的所有文本/代碼/標記,並複製到使用 include 語句的文件中。
語法:
?php include ‘filename’; ?
require語句和include的功能是相同的,只是在錯誤處理方面不一樣。
require 會生成致命錯誤(E_COMPILE_ERROR)並停止腳本。
include 只生成警告(E_WARNING),並且腳本會繼續。
擴展資料:
php語句
include()語句包含並運行指定文件。
以下文檔也適用於require()。這兩種結構除了在如何處理失敗之外完全一樣。include()產生一個警告而require()則導致一個致命錯誤。
換句話說,如果想在遇到丟失文件時停止處理頁面就用require()。include()就不是這樣,腳本會繼續運行。同時也要確認設置了合適的 include_path。注意在 php 4.3.5 之前,包含文件中的語法錯誤不會導致程序停止,但從此版本之後會。
參考資料來源:百度百科-include (計算機專業術語)
include的php語句
include()語句包含並運行指定文件。
以下文檔也適用於require()。這兩種結構除了在如何處理失敗之外完全一樣。include()產生一個警告而require()則導致一個致命錯誤。換句話說,如果想在遇到丟失文件時停止處理頁面就用require()。include()就不是這樣,腳本會繼續運行。同時也要確認設置了合適的 include_path。注意在 php 4.3.5 之前,包含文件中的語法錯誤不會導致程序停止,但從此版本之後會。
尋找包含文件的順序先是在當前工作目錄的相對的 include_path 下尋找,然後是當前運行腳本所在目錄相對的 include_path 下尋找。例如 include_path 是 .,當前工作目錄是 /www/,腳本中要 include 一個 include/a.php 並且在該文件中有一句 include b.php,則尋找 b.php 的順序先是 /www/,然後是 /www/include/。如果文件名以 ../ 開始,則只在當前工作目錄相對的 include_path 下尋找。
當一個文件被包含時,其中所包含的代碼繼承了 include 所在行的變量範圍。從該處開始,調用文件在該行處可用的任何變量在被調用的文件中也都可用。不過所有在包含文件中定義的函數和類都具有全局作用域。
例子 16-5. 基本的include()例子
vars.php?php$color = ‘green’;$fruit = ‘apple’;?test.php?phpecho A $color $fruit; // Ainclude ‘vars.php’;echo A $color $fruit; // A green apple? 如果 include 出現於調用文件中的一個函數里,則被調用的文件中所包含的所有代碼將表現得如同它們是在該函數內部定義的一樣。所以它將遵循該函數的變量範圍。
例子 16-6. 函數中的包含
?phpfunction 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 appleecho A $color $fruit; // A green?例子 16-7. 通過 HTTP 進行的include()
?php/* This example assumes that 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 as phpinclude ‘/file.txt?foo=1bar=2’;// Won’t work; looks for a file named ‘file.php?foo=1bar=2’ on the// local filesystem.include ‘file.php?foo=1bar=2’;// Works.include ‘/file.php?foo=1bar=2’;$foo = 1;$bar = 2;include ‘file.txt’; // Works.include ‘file.php’; // Works.?相關信息參見使用遠程文件,fopen()和file()。
因為include()和require()是特殊的語言結構,在條件語句中使用必須將其放在語句組中(花括號中)。
例子 16-8. 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 開始和結束標記(如同任何本地文件一樣)。可以在標記內定義所需的變量,該變量在文件被包含的位置之後就可用了。
因為include()是一個特殊的語言結構,其參數不需要括號。在比較其返回值時要注意。
例子 16-9. 比較 include 的返回值
?php// won’t work, evaluated as include((‘vars.php’) == ‘OK’), i.e. include(”)if (include(‘vars.php’) == ‘OK’) { echo ‘OK’;}// worksif ((include ‘vars.php’) == ‘OK’) { echo ‘OK’;}?注:在 php 3 中,除非是在函數中調用否則被包含的文件中不能出現 return。在此情況下return()作用於該函數而不是整個文件。
例子 16-10.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()而另一個沒有。如果文件不能被包含,則返回FALSE並發出一個 E_WARNING 警告。
如果在包含文件中定義有函數,這些函數可以獨立於是否在return()之前還是之後在主文件中使用。如果文件被包含兩次,php 5 發出致命錯誤因為函數已經被定義,但是 php 在return()之後不會抱怨函數已定義。推薦使用include_once()而不是檢查文件是否已包含並在包含文件中有條件返回。
另一個將 php 文件“包含”到一個變量中的方法是用輸出控制函數結合include()來捕獲其輸出,例如:
例子 16-11. 使用輸出緩衝來將 php 文件包含入一個字符串
?php$string = get_include_contents(‘somefile.php’);function get_include_contents($filename) { if (is_file($filename)) { ob_start(); include $filename; $contents = ob_get_contents(); ob_end_clean(); return $contents; } return false;}?要在腳本中自動包含文件,參見 php.ini 中的 auto_prepend_file 和 auto_append_file 配置選項。
注:由於這是一個語言結構而非函數,因此它無法被變量函數調用。
原創文章,作者:DFXP,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/141551.html