PHP中include和require的区别实例分析
网络编程 2021-07-05 08:24www.168986.cn编程入门
网上太多关于PHP中include与require区别。事实真的如此吗,今天我们就通过一个具体的实例来简单分析验证下
先编辑mand.php文件
echo 'hello'.PHP_EOL;
然后编辑console.php文件
for($i=1;$i<=3;++$i){ require 'mand1.php'; }
原本想要包含并执行这个echo,没想到写错了文件名,如果是require,会报出这样的错误
Warning: require(mand1.php): failed to open stream: No such file or directory in console.php on line 4 Fatal error: require(): Failed opening required 'mand1.php' (include_path='.') in console.php on line 4 PHP Warning: require(mand1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Fatal error: require(): Failed opening required 'mand1.php' (include_path='.') in console.php on line 4
如果把require改为include
for($i=1;$i<=3;++$i){ include 'mand1.php'; }
会报出这样的错误
Warning: include(mand1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'mand1.php' for inclusion (include_path='.') in console.php on line 4 Warning: include(mand1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'mand1.php' for inclusion (include_path='.') in console.php on line 4 Warning: include(mand1.php): failed to open stream: No such file or directory in console.php on line 4 Warning: include(): Failed opening 'mand1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(mand1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'mand1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(mand1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'mand1.php' for inclusion (include_path='.') in console.php on line 4 PHP Warning: include(mand1.php): failed to open stream: No such file or directory in console.php on line 4 PHP Warning: include(): Failed opening 'mand1.php' for inclusion (include_path='.') in console.php on line 4
如果使用require_once或者include_once,只要包含路径正确,那么循环只执行一次。
使用require,如果文件没有包含成功,就会报出一个fatal error,整个程序就中止了。
使用include,如果文件没有包含成功,就会报出一个普通的warning,之后的代码仍会执行。
如果你的Web程序使用了MVC这种对文件包含强依赖的设计方法,请使用require_once。
上一篇:Laravel实现autoload方法详解
下一篇:自制PHP框架之设计模式
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程