php中如何引入文件?相信很多新手都還沒(méi)學(xué)會(huì)這個(gè)技能,為此給大家總結(jié)了這篇關(guān)于php引入文件的文章,話不多說(shuō),一起往下看吧。
成都創(chuàng)新互聯(lián)公司專注于高郵企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),成都商城網(wǎng)站開(kāi)發(fā)。高郵網(wǎng)站建設(shè)公司,為高郵等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
PHP中引入文件的方法有:include、require、include_once、require_once。
區(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 test2\n'; function test() { echo 'this is test\n'; } ?> //結(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 test2\n'; function test() { echo 'this is test\n'; } ?>
結(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ū)別相同。
以上就是php引入文件的方法,圖文詳解容易學(xué)習(xí),配合代碼閱讀理解效果更佳,非常適合初學(xué)者入門(mén),如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊!
分享文章:php中如何引入文件?
瀏覽地址:http://aaarwkj.com/article38/ggphsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、建站公司、云服務(wù)器、微信小程序、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)