php引用文件有4種方式:1、使用include()函數(shù),可以放在PHP腳本的任意位置;2、require()函數(shù),一般放在PHP腳本的最前面;3、include_once()函數(shù);4、require_once()函數(shù)。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
PHP中有四個(gè)加載文件的語(yǔ)句:include、require、include_once、require_once。
基本語(yǔ)法
require:require函數(shù)一般放在PHP腳本的最前面,PHP執(zhí)行前就會(huì)先讀入require指定引入的文件,包含并嘗試執(zhí)行引入的腳本文件。require的工作方式是提高PHP的執(zhí)行效率,當(dāng)它在同一個(gè)網(wǎng)頁(yè)中解釋過(guò)一次后,第二次便不會(huì)解釋。但同樣的,正因?yàn)樗粫?huì)重復(fù)解釋引入文件,所以當(dāng)PHP中使用循環(huán)或條件語(yǔ)句來(lái)引入文件時(shí),需要用到include。
include:可以放在PHP腳本的任意位置,一般放在流程控制的處理部分中。當(dāng)PHP腳本執(zhí)行到include指定引入的文件時(shí),才將它包含并嘗試執(zhí)行。這種方式可以把程序執(zhí)行時(shí)的流程進(jìn)行簡(jiǎn)單化。當(dāng)?shù)诙斡龅较嗤募r(shí),PHP還是會(huì)重新解釋一次,include相對(duì)于require的執(zhí)行效率下降很多,同時(shí)在引入文件中包含用戶自定義函數(shù)時(shí),PHP在解釋過(guò)程中會(huì)發(fā)生函數(shù)重復(fù)定義問(wèn)題。
require_once / include_once:分別與require / include作用相同,不同的是他們?cè)趫?zhí)行到時(shí)會(huì)先檢查目標(biāo)內(nèi)容是不是在之前已經(jīng)導(dǎo)入過(guò),如果導(dǎo)入過(guò)了,那么便不會(huì)再次重復(fù)引入其同樣的內(nèi)容。
相互區(qū)別
include和require:
include有返回值,而require沒(méi)有返回值
include在加載文件失敗時(shí),會(huì)生成一個(gè)警告(E_WARNING),在錯(cuò)誤發(fā)生后腳本繼續(xù)執(zhí)行。所以include用在希望繼續(xù)執(zhí)行并向用戶輸出結(jié)果時(shí)。
//test1.php <?php include './tsest.php'; echo 'this is test1'; ?> //test2.php <?php echo 'this is test2n'; function test() { echo 'this is testn'; } ?> //結(jié)果: this is test1
require在加載失敗時(shí)會(huì)生成一個(gè)致命錯(cuò)誤(E_COMPILE_ERROR),在錯(cuò)誤發(fā)生后腳本停止執(zhí)行。一般用在后續(xù)代碼依賴于載入的文件的時(shí)候。
//test1.php <?php require './tsest.php'; echo 'this is test1'; ?> //test2.php <?php echo 'this is test2n'; function test() { echo 'this is testn'; } ?>
結(jié)果:
include和include_once:
include載入的文件不會(huì)判斷是否重復(fù),只要有include語(yǔ)句,就會(huì)載入一次(即使可能出現(xiàn)重復(fù)載入)。而include_once載入文件時(shí)會(huì)有內(nèi)部判斷機(jī)制判斷前面代碼是否已經(jīng)載入過(guò)。這里需要注意的是include_once是根據(jù)前面有無(wú)引入相同路徑的文件為判斷的,而不是根據(jù)文件中的內(nèi)容(即兩個(gè)待引入的文件內(nèi)容相同,使用include_once還是會(huì)引入兩個(gè))。
//test1.php <?php include './test2.php'; echo 'this is test1'; include './test2.php'; ?> //test2.php <?php echo 'this is test2'; ?> //結(jié)果: 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'; ?> //結(jié)果: 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'; ?> //結(jié)果: 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'; ?> //結(jié)果: this is test2this is test1
require和require_once:同include和include_once的區(qū)別相同。
載入時(shí)執(zhí)行過(guò)程
1. 從include(require)語(yǔ)句退出php腳本模式(進(jìn)入html代碼模式)
2. 載入include語(yǔ)句所設(shè)定的文件中的代碼,并嘗試執(zhí)行
3. 退出html模式,重新進(jìn)入php腳本模式,繼續(xù)后面腳本程序的執(zhí)行
//test1.php <html> <body> 主文件開始位置: <?php echo "<br> 主文件中位置 A"; include "./test2.php"; //要載入的文件 echo "<br> 主文件中位置 B"; ?> <br> 主文件結(jié)束位置 </body> </html> //test2.php <br> 被載入文件位置 1 <?php echo "<br> 被載入文件位置 2"; ?> <br> 被載入文件位置 3
結(jié)果:
分析:
返回值的比較
上文說(shuō)道include有返回值,而require無(wú)返回值
對(duì)于include,如果載入成功,有返回值,返回值為1;如果載入失敗,則返回false.
對(duì)于require,如果載入成功,有返回值,返回值為1;如果載入失敗,無(wú)返回值。
//test1.php <?php $a = include "./test2.php"; var_dump($a); echo "<br>"; $b = include "./test2.phps"; var_dump($b); echo "<br>"; $c = require "./test2.php"; var_dump($c); echo "<br>"; $d = require "./test2.phps"; var_dump($d); ?>
輸出:
當(dāng)文件中有return:
當(dāng)被載入文件中有return語(yǔ)句時(shí),會(huì)有另外的機(jī)制,此時(shí)return語(yǔ)句的作用是終止載入過(guò)程,即被載入文件中return語(yǔ)句的后續(xù)代碼不再載入。return語(yǔ)句也可以用于被載入文件載入時(shí)返回一個(gè)數(shù)據(jù)。
//test1.php <?php $a = include "./test2.php"; echo "<br>"; var_dump($a); ?> //test2.php //該文件中有return語(yǔ)句 <?php $b = 'test2'; echo "被載入的文件:A 位置"; return $b; echo "<br 被載入的文件: B 位置"; ?>
結(jié)果:
推薦學(xué)習(xí):《PHP視頻教程》