最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP中include和require的區(qū)別實(shí)例分析

 更新時間:2017年05月07日 10:40:56   作者:編程老頭  
網(wǎng)上太多關(guān)于PHP中include與require區(qū)別。然而事實(shí)真的如此嗎,今天我們就通過一個具體的實(shí)例來簡單分析驗(yàn)證下

先編輯command.php文件

echo 'hello'.PHP_EOL;

然后編輯console.php文件

for($i=1;$i<=3;++$i){
	require 'command1.php';
}

原本想要包含并執(zhí)行這個echo,沒想到寫錯了文件名,如果是require,會報(bào)出這樣的錯誤:

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';
}

會報(bào)出這樣的錯誤:

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,如果文件沒有包含成功,就會報(bào)出一個fatal error,整個程序就中止了。

使用include,如果文件沒有包含成功,就會報(bào)出一個普通的warning,之后的代碼仍會執(zhí)行。

如果你的Web程序使用了MVC這種對文件包含強(qiáng)依賴的設(shè)計(jì)方法,請使用require_once。

相關(guān)文章

最新評論

黄龙县| 高唐县| 奉节县| 新闻| 栾城县| 车致| 富宁县| 邵阳市| 石狮市| 芦溪县| 阿拉善盟| 屏东市| 兴义市| 龙江县| 华池县| 石狮市| 怀来县| 娄底市| 新河县| 沙洋县| 二手房| 靖安县| 尚义县| 九江市| 社会| 诸暨市| 和田市| 汪清县| 江北区| 富蕴县| 安国市| 荣成市| 财经| 绍兴市| 宁陵县| 纳雍县| 新源县| 满城县| 汤原县| 乐安县| 固镇县|