站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        php引用文件有幾種方式

        php引用文件有4種方式:1、使用include()函數(shù),可以放在PHP腳本的任意位置;2、require()函數(shù),一般放在PHP腳本的最前面;3、include_once()函數(shù);4、require_once()函數(shù)。

        php引用文件有幾種方式

        本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

        PHP中有四個(gè)加載文件的語句:include、require、include_once、require_once

        基本語法

        require:require函數(shù)一般放在PHP腳本的最前面,PHP執(zhí)行前就會(huì)先讀入require指定引入的文件,包含并嘗試執(zhí)行引入的腳本文件。require的工作方式是提高PHP的執(zhí)行效率,當(dāng)它在同一個(gè)網(wǎng)頁中解釋過一次后,第二次便不會(huì)解釋。但同樣的,正因?yàn)樗粫?huì)重復(fù)解釋引入文件,所以當(dāng)PHP中使用循環(huán)或條件語句來引入文件時(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在解釋過程中會(huì)發(fā)生函數(shù)重復(fù)定義問題。

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

        相互區(qū)別

        include和require:

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

        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é)果:

        php引用文件有幾種方式

        include和include_once:

        include載入的文件不會(huì)判斷是否重復(fù),只要有include語句,就會(huì)載入一次(即使可能出現(xiàn)重復(fù)載入)。而include_once載入文件時(shí)會(huì)有內(nèi)部判斷機(jī)制判斷前面代碼是否已經(jīng)載入過。這里需要注意的是include_once是根據(jù)前面有無引入相同路徑的文件為判斷的,而不是根據(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í)行過程

        1. 從include(require)語句退出php腳本模式(進(jìn)入html代碼模式)

        2. 載入include語句所設(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é)果:

        php引用文件有幾種方式

        分析:

        php引用文件有幾種方式

        返回值的比較

        上文說道include有返回值,而require無返回值

        對(duì)于include,如果載入成功,有返回值,返回值為1;如果載入失敗,則返回false.

        對(duì)于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);  ?>

        輸出:

        php引用文件有幾種方式

        當(dāng)文件中有return:

        當(dāng)被載入文件中有return語句時(shí),會(huì)有另外的機(jī)制,此時(shí)return語句的作用是終止載入過程,即被載入文件中return語句的后續(xù)代碼不再載入。return語句也可以用于被載入文件載入時(shí)返回一個(gè)數(shù)據(jù)。

        //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 位置"; ?>

        結(jié)果:

        php引用文件有幾種方式

        推薦學(xué)習(xí):《PHP視頻教程》

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 欧美午夜精品一区二区三区91| 中文字幕日韩精品无码内射| 四虎国产精品免费久久| 99精品免费视频| 亚洲国产精品专区在线观看| 国产精品美女WWW爽爽爽视频| 国产欧美精品一区二区三区| 日本精品视频在线观看| 国产成人综合久久精品尤物| 国产精品一在线观看| 久久精品中文闷骚内射| 无码国内精品久久人妻麻豆按摩 | 99久久精品毛片免费播放| 少妇人妻无码精品视频| 亚洲精品tv久久久久久久久久| 久久国产精品视频| 久久精品国产99久久香蕉| 国产精品视频全国免费观看 | 久久99国产精品久久| 国产精品美女久久久久| 老司机亚洲精品影院| 亚洲精品无码Av人在线观看国产| 免费精品精品国产欧美在线| 久久精品国产99久久丝袜| 久久精品中文字幕有码| 精品国产乱码久久久久久浪潮| 99久久国产主播综合精品| 久久九九亚洲精品| 亚洲精品视频在线| 91不卡在线精品国产| 成人午夜精品亚洲日韩| 国产精品第1页| 精品人妻无码专区中文字幕| 精品综合久久久久久88小说| 久久久99精品成人片中文字幕| 日韩精品欧美激情国产一区| 日韩精品无码免费视频 | 乱人伦人妻精品一区二区| 久久精品亚洲男人的天堂| 欧美激情精品久久久久久| 四虎精品成人免费视频|