在网上看到很多资料,说include和require的区别大致有这么三个。
1.require在无论任何条件下都强制调用。
2.require没有返回值
3.require在错误的情况下,会导致一个致命错误,并停止处理页面。而include只是产生一个警告,继续处理页面。
大家对require的印象就是处理速度较快,在大多数情况下都采用include。
当我看到那些文章后,进行一一测试,非常不凑巧,网上的那些文章都得不到很好的验证。
我用的是php5,所以我猜想可能php5对include和require进行了调整吧。
查了下手册最后才知道:
You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.
The two functions are identical in every way, except how they handle errors:
include() generates a warning, but the script will continue execution
require() generates a fatal error, and the script will stop
It is recommended to use the require() function instead of include(), because scripts should not continue after an error.
这下清楚了!
这两种结构除了在如何处理失败之外完全一样。include() 产生一个警告而require()则导致一个致命错误。换句话说,如果你想在遇到丢失文件时停止处理页面就用require()。include()就不是这样,脚本会继续运行。官方推荐使用require()。