国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

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

2024-05-04 23:57:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

先編輯command.php文件

echo 'hello'.PHP_EOL;

然后編輯console.php文件

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

原本想要包含并執(zhí)行這個(gè)echo,沒(méi)想到寫(xiě)錯(cuò)了文件名,如果是require,會(huì)報(bào)出這樣的錯(cuò)誤:

Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4Fatal error: require(): Failed opening required 'command1.php' (include_path='.') in console.php on line 4PHP Warning: require(command1.php): failed to open stream: No such file or directory in console.php on line 4PHP 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 4Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4PHP Warning: include(): Failed opening 'command1.php' for inclusion (include_path='.') in console.php on line 4PHP Warning: include(command1.php): failed to open stream: No such file or directory in console.php on line 4PHP 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,如果文件沒(méi)有包含成功,就會(huì)報(bào)出一個(gè)fatal error,整個(gè)程序就中止了。

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

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 托克托县| 芷江| 登封市| 屏东县| 故城县| 吴忠市| 长治县| 定远县| 攀枝花市| 临猗县| 永和县| 高州市| 荥经县| 思南县| 平南县| 亚东县| 罗定市| 文成县| 边坝县| 昌乐县| 长武县| 永年县| 邮箱| 姜堰市| 珠海市| 通辽市| 东港市| 靖安县| 宁都县| 盐津县| 屯昌县| 肇源县| 五台县| 晴隆县| 含山县| 吴忠市| 会泽县| 昆山市| 台中市| 甘德县| 泗洪县|