《PHP实例:PHP中include和require的区别实例分析》要点:
本文介绍了PHP实例:PHP中include和require的区别实例分析,希望对您有用。如果有疑问,可以联系我们。
先编辑command.php文件PHP教程
echo 'hello'.PHP_EOL;
然后编辑console.php文件PHP教程
for($i=1;$i<=3;++$i){ require 'command1.php'; }
原本想要包含并执行这个echo,没想到写错了文件名,如果是require,会报出这样的错误:PHP教程
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改为includePHP教程
for($i=1;$i<=3;++$i){ include 'command1.php'; }
会报出这样的错误: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,只要包含路径正确,那么循环只执行一次.PHP教程
总结:PHP教程
使用require,如果文件没有包含成功,就会报出一个fatal error,整个程序就中止了.PHP教程
使用include,如果文件没有包含成功,就会报出一个普通的warning,之后的代码仍会执行.PHP教程
如果你的Web程序使用了MVC这种对文件包含强依赖的设计方法,请使用require_once.PHP教程
转载请注明本页网址:
http://www.vephp.com/jiaocheng/830.html