php php中“include”和“require”的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3633900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between "include" and "require" in php
提问by Dan Hanly
Is there any difference between them? Is using them a matter of preference? Does using one over the other produce any advantages? Which is better for security?
它们之间有什么区别吗?使用它们是偏好问题吗?使用其中一种会产生任何优势吗?哪个更安全?
采纳答案by Steven
You find the differences explained in the detailed PHP manual on the page of require
:
您可以在以下页面require
的详细 PHP 手册中找到解释的差异:
require
is identical toinclude
except upon failure it will also produce a fatalE_COMPILE_ERROR
level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING
) which allows the script to continue.
require
与 相同,include
除了在失败时它也会产生致命的E_COMPILE_ERROR
级别错误。换句话说,它将停止脚本,而 include 仅发出警告 (E_WARNING
) 允许脚本继续。
See @efritz's answerfor an example
有关示例,请参阅@efritz 的答案
回答by efritz
require
will throw a PHP Fatal Error if the file cannot be loaded. (Execution stops)
require
如果无法加载文件,将抛出 PHP 致命错误。(执行停止)
include
produces a Warning if the file cannot be loaded. (Execution continues)
include
如果无法加载文件,则会产生警告。(执行继续)
Here is a nice illustration of include and require difference:
这是include 和 require 差异的一个很好的说明:
From:Difference require vs. include php (by Robert; Nov 2012)
回答by Martin Bean
Use include
if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed.
使用include
,如果你不介意你的脚本继续而不加载文件(如果它不存在等),你可以(虽然你不应该)现场用警告的错误信息被显示。
Using require
means your script will halt if it can't load the specified file, and throw a Fatal error.
使用require
意味着如果您的脚本无法加载指定的文件,您的脚本将停止,并引发致命错误。
回答by user187291
As others pointed out, the only difference is that require throws a fatal error, and include - a catchable warning. As for which one to use, my advice is to stick to include. Why? because you can catch a warning and produce a meaningful feedback to end users. Consider
正如其他人指出的那样,唯一的区别是 require 抛出一个致命错误,并包含 - 一个可捕获的警告。至于使用哪个,我的建议是坚持包含。为什么?因为您可以捕捉警告并向最终用户提供有意义的反馈。考虑
// Example 1.
// users see a standard php error message or a blank screen
// depending on your display_errors setting
require 'not_there';
// Example 2.
// users see a meaningful error message
try {
include 'not_there';
} catch(Exception $e) {
echo "something strange happened!";
}
NB: for example 2 to work you need to install an errors-to-exceptions handler, as described here http://www.php.net/manual/en/class.errorexception.php
注意:例如 2 工作你需要安装一个错误到异常处理程序,如这里所述http://www.php.net/manual/en/class.errorexception.php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
回答by Tara Prasad Gurung
<?PHP
echo "Firstline";
include('classes/connection.php');
echo "I will run if include but not on Require";
?>
A very simple Practical example with code.The first echo will be displayed. No matter you use include or require because its runs before include or required.
一个非常简单的带有代码的实用示例。将显示第一个回声。无论您使用 include 还是 require 都是因为它在 include 或 required 之前运行。
To check the result, In second line of a code intentionally provide the wrong path to the file or make error in file name. Thus the second echo to be displayed or not will be totally dependent on whether you use requireor include.
为了检查结果,在代码的第二行故意提供错误的文件路径或在文件名中出错。因此,要显示或不显示的第二个回声将完全取决于您是使用require还是include。
If you use requirethe second echo will not execute but if you use includenot matter what error comes you will see the result of second echo too.
如果您使用require第二个 echo 将不会执行但如果您使用include无论出现什么错误,您也会看到第二个 echo 的结果。
回答by Tara Prasad Gurung
In case of Include Program will not terminate and display warning on browser,On the other hand Require program will terminate and display fatal error in case of file not found.
如果 Include 程序不会终止并在浏览器上显示警告,另一方面 Require 程序将在找不到文件的情况下终止并显示致命错误。