主頁 > 知識庫 > php文件包含的幾種方式總結

php文件包含的幾種方式總結

熱門標簽:科大訊飛語音識別系統(tǒng) 團購網(wǎng)站 電子圍欄 Linux服務器 銀行業(yè)務 服務器配置 Mysql連接數(shù)設置 阿里云

四種語句

PHP中有四個加載文件的語句:include、require、include_once、require_once。

基本語法

require:require函數(shù)一般放在PHP腳本的最前面,PHP執(zhí)行前就會先讀入require指定引入的文件,包含并嘗試執(zhí)行引入的腳本文件。require的工作方式是提高PHP的執(zhí)行效率,當它在同一個網(wǎng)頁中解釋過一次后,第二次便不會解釋。但同樣的,正因為它不會重復解釋引入文件,所以當PHP中使用循環(huán)或條件語句來引入文件時,需要用到include。

include:可以放在PHP腳本的任意位置,一般放在流程控制的處理部分中。當PHP腳本執(zhí)行到include指定引入的文件時,才將它包含并嘗試執(zhí)行。這種方式可以把程序執(zhí)行時的流程進行簡單化。當?shù)诙斡龅较嗤募r,PHP還是會重新解釋一次,include相對于require的執(zhí)行效率下降很多,同時在引入文件中包含用戶自定義函數(shù)時,PHP在解釋過程中會發(fā)生函數(shù)重復定義問題。

require_once / include_once:分別與require / include作用相同,不同的是他們在執(zhí)行到時會先檢查目標內(nèi)容是不是在之前已經(jīng)導入過,如果導入過了,那么便不會再次重復引入其同樣的內(nèi)容。

相互區(qū)別

include和require:

include有返回值,而require沒有返回值。

include在加載文件失敗時,會生成一個警告(E_WARNING),在錯誤發(fā)生后腳本繼續(xù)執(zhí)行。所以include用在希望繼續(xù)執(zhí)行并向用戶輸出結果時。

//test1.php

?php

include './tsest.php';

echo 'this is test1';

?>

 

//test2.php

?php

echo 'this is test2\n';

function test() {

 echo 'this is test\n';

}

?>

 

//結果:

this is test1

require在加載失敗時會生成一個致命錯誤(E_COMPILE_ERROR),在錯誤發(fā)生后腳本停止執(zhí)行。一般用在后續(xù)代碼依賴于載入的文件的時候。

//test1.php

?php

require './tsest.php';

echo 'this is test1';

?>

 

//test2.php

?php

echo 'this is test2\n';

function test() {

 echo 'this is test\n';

}

?>

結果:

include和include_once:

include載入的文件不會判斷是否重復,只要有include語句,就會載入一次(即使可能出現(xiàn)重復載入)。而include_once載入文件時會有內(nèi)部判斷機制判斷前面代碼是否已經(jīng)載入過。這里需要注意的是include_once是根據(jù)前面有無引入相同路徑的文件為判斷的,而不是根據(jù)文件中的內(nèi)容(即兩個待引入的文件內(nèi)容相同,使用include_once還是會引入兩個)。

//test1.php

?php

include './test2.php';

echo 'this is test1';

include './test2.php';

?>

 

//test2.php

?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1this is test2

 

 

//test1.php

?php

include './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

 

//test2.php

?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1

 

 

//test1.php

?php

include_once './test2.php';

echo 'this is test1';

include './test2.php';

?>

 

//test2.php

?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1this is test2

 

 

//test1.php

?php

include_once './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

 

//test2.php

?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1

require和require_once:同include和include_once的區(qū)別相同。

以上就是本次介紹的全部知識點內(nèi)容,感謝大家對腳本之家的支持。

您可能感興趣的文章:
  • 使用SMB共享來繞過php遠程文件包含的限制執(zhí)行RFI的利用
  • php文件包含目錄配置open_basedir的使用與性能詳解
  • PHP中你應該知道的require()文件包含的正確用法
  • php 偽造本地文件包含漏洞的代碼
  • PHP 網(wǎng)絡開發(fā)詳解之遠程文件包含漏洞

標簽:大理 蚌埠 廣元 萍鄉(xiāng) 衡水 棗莊 江蘇 衢州

巨人網(wǎng)絡通訊聲明:本文標題《php文件包含的幾種方式總結》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266