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

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

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

先編輯command.php文件

echo 'hello'.PHP_EOL;

然后編輯console.php文件

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

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

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

會報出這樣的錯誤:

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

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

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

相關(guān)文章

最新評論

西藏| 库伦旗| 绥宁县| 榆林市| 涟水县| 滦南县| 卫辉市| 莱芜市| 安阳市| 淅川县| 许昌县| 梁山县| 陇南市| 塔河县| 江北区| 普陀区| 八宿县| 永州市| 淮北市| 宁晋县| 乌拉特前旗| 五原县| 睢宁县| 南召县| 措勤县| 乌海市| 丽水市| 石泉县| 张北县| 新巴尔虎左旗| 衡南县| 庆元县| 呼玛县| 桑植县| 蒲江县| 湖口县| 衡阳市| 湘乡市| 合肥市| 栾城县| 安义县|