PHP中include和require的区别实例分析

 更新时间:2017年05月07日 10:40:56   作者:编程老头  
网上太多关于PHP中include与require区别。然而事实真的如此吗,今天我们就通过一个具体的实例来简单分析验证下

先编辑command.php文件

echo 'hello'.PHP_EOL;

然后编辑console.php文件

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

原本想要包含并执行这个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,只要包含路径正确,那么循环只执行一次。

总结:

使用require,如果文件没有包含成功,就会报出一个fatal error,整个程序就中止了。

使用include,如果文件没有包含成功,就会报出一个普通的warning,之后的代码仍会执行。

如果你的Web程序使用了MVC这种对文件包含强依赖的设计方法,请使用require_once。

相关文章

  • array_values()在php中返回数组的操作实例

    array_values()在php中返回数组的操作实例

    在本篇文章里小编给大家分享的是一篇关于array_values()在php中返回数组的操作实例内容,有兴趣的朋友们可以学习参考下。
    2021-02-02
  • php将从数据库中获得的数据转换成json格式并输出的方法

    php将从数据库中获得的数据转换成json格式并输出的方法

    今天小编就为大家分享一篇php将从数据库中获得的数据转换成json格式并输出的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • php curl_init函数用法

    php curl_init函数用法

    使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了
    2014-01-01
  • php 将excel导入mysql

    php 将excel导入mysql

    最近因项目需求,要实现将excel文件通过php页面导入mysql数据库中。在网上搜了很多这方面的资料,发现都是将excel文件另存为csv文件,然后从csv文件导入。
    2009-11-11
  • 浅谈php提交form表单

    浅谈php提交form表单

    这篇文章主要介绍了浅谈php提交form表单的2种方法和简单的示例,十分的实用,有需要的小伙伴可以参考下。
    2015-07-07
  • PHP实现QQ空间自动回复说说的方法

    PHP实现QQ空间自动回复说说的方法

    这篇文章主要介绍了PHP实现QQ空间自动回复说说的方法,涉及php基于curl调用自动回复接口的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-12-12
  • 使用PHP获取网络文件的实现代码

    使用PHP获取网络文件的实现代码

    PHP获取网络文件的实现代码,其实就是一个小偷程序。学习php小偷程序的朋友可以参考下。
    2010-01-01
  • 如何让PHP编码更加好看利于阅读

    如何让PHP编码更加好看利于阅读

    在本篇文章里小编给大家分享了关于如何让PHP编码更加好看利于阅读的方法和习惯,需要的朋友们可以学习下。
    2019-05-05
  • php中flush()、ob_flush()、ob_end_flush()的区别介绍

    php中flush()、ob_flush()、ob_end_flush()的区别介绍

    php中flush()、ob_flush()、ob_end_flush()的区别介绍,需要的朋友可以参考下
    2013-02-02
  • php 中的信号处理操作实例详解

    php 中的信号处理操作实例详解

    这篇文章主要介绍了php 中的信号处理操作,结合实例形式详细分析了php 信号处理相关函数、原理、使用方法及操作注意事项,需要的朋友可以参考下
    2020-03-03

最新评论