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