PHP中include和require的區(qū)別實(shí)例分析
先編輯command.php文件
echo 'hello'.PHP_EOL;
然后編輯console.php文件
for($i=1;$i<=3;++$i){ require 'command1.php'; }
原本想要包含并執(zhí)行這個(gè)echo,沒想到寫錯(cuò)了文件名,如果是require,會(huì)報(bào)出這樣的錯(cuò)誤:
Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4 Fatal error: require(): Failed opening required 'command1.php' (include_path='.') in console.php on line 4 PHP Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Fatal error: require(): Failed opening required 'command1.php' (include_path='.') in console.php on line 4
如果把require改為include
for($i=1;$i<=3;++$i){ include 'command1.php'; }
會(huì)報(bào)出這樣的錯(cuò)誤:
Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4
如果使用require_once或者include_once,只要包含路徑正確,那么循環(huán)只執(zhí)行一次。
總結(jié):
使用require,如果文件沒有包含成功,就會(huì)報(bào)出一個(gè)fatal error,整個(gè)程序就中止了。
使用include,如果文件沒有包含成功,就會(huì)報(bào)出一個(gè)普通的warning,之后的代碼仍會(huì)執(zhí)行。
如果你的Web程序使用了MVC這種對(duì)文件包含強(qiáng)依賴的設(shè)計(jì)方法,請(qǐng)使用require_once。
- php使用include 和require引入文件的區(qū)別
- PHP中include()與require()的區(qū)別說明
- PHP中include/require/include_once/require_once使用心得
- 簡(jiǎn)單談?wù)凱HP中的include、include_once、require以及require_once語句
- PHP中require和include路徑問題詳解
- PHP包含文件函數(shù)include、include_once、require、require_once區(qū)別總結(jié)
- PHP中include與require使用方法區(qū)別詳解
- php include和require的區(qū)別深入解析
- PHP中include()與require()的區(qū)別說明
- PHP中include和require的使用詳解
相關(guān)文章
PHP實(shí)現(xiàn)的限制IP投票程序IP來源分析
這篇文章主要介紹了PHP實(shí)現(xiàn)的限制IP投票程序,針對(duì)投票程序中客戶端IP的判斷方法進(jìn)行了深入分析,并給出了偽造IP的方法供大家參考了解,需要的朋友可以參考下2016-05-05PHP flush 函數(shù)使用注意事項(xiàng)
ob_flush/flush在手冊(cè)中的描述, 都是刷新輸出緩沖區(qū), 并且還需要配套使用, 所以會(huì)導(dǎo)致很多人迷惑…其實(shí), 他們倆的操作對(duì)象不同, 有些情況下, flush根本不做什么事情2016-08-08php array_merge函數(shù)使用需要注意的一個(gè)問題
這篇文章主要介紹了php array_merge函數(shù)使用需要注意的一個(gè)問題,本文講解array_merge合并數(shù)組時(shí)不會(huì)合并數(shù)字鍵名的問題,大家使用時(shí)需要注意,需要的朋友可以參考下2015-03-03PHP中for循環(huán)與foreach的區(qū)別
本文主要介紹了php中for循環(huán)與foreach的區(qū)別,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03php學(xué)習(xí)之?dāng)?shù)據(jù)類型之間的轉(zhuǎn)換介紹
php學(xué)習(xí)之?dāng)?shù)據(jù)類型之間的轉(zhuǎn)換介紹,學(xué)習(xí)php的朋友可以參考下。2011-06-06