PHP 中的“include_once”和“require_once”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21814296/
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
What is difference between "include_once" and "require_once" in PHP?
提问by FarwallGhost
What is difference between "include_once" and "require_once" in PHP?
PHP 中的“include_once”和“require_once”有什么区别?
include_once "connect_to_mysql.php";
require_once "connect_to_mysql.php";
Is there any difference between these?
这些有什么区别吗?
回答by Norguard
Include will let the script keep running (with a warning) if the file is missing.
如果文件丢失,包含将让脚本继续运行(带有警告)。
Require will crash if it's missing.
如果缺少,Require 将崩溃。
If you're using include for things which are 100% optional, then include will still work and require will explode.
如果您将 include 用于 100% 可选的内容,那么 include 仍然有效,而 require 会爆炸。
If you include something that you think is optional, but some other piece of your script uses it, way down the line, then your script will explode there, and you'll probably have no idea why.
如果您包含一些您认为是可选的内容,但您的脚本的其他部分使用它,那么您的脚本将在那里爆炸,您可能不知道为什么。
This is not a good way to write programs.
这不是编写程序的好方法。
Otherwise, there isn't a difference between the two.
否则,两者之间没有区别。
edit
编辑
In typical usage, it shouldn't matter whether you choose to use includeor require, 95% of the time.
在典型的使用中,在95% 的情况下,您是选择使用include还是应该无关紧要require。
As such, you should stick to require(or require_once), to tell you when you're missing a file you need.
因此,您应该坚持使用require(或require_once),告诉您何时丢失了您需要的文件。
The bugs that come from including files inside of included files, inside of included files, when one include up top is missing, are really hard to track down.
由于在被包含文件中包含文件、在包含文件中包含文件而导致的错误,当一个包含顶部丢失时,真的很难追踪。
Some people prefer include, because they want to use that "feature".
Douglas Crockford is a JavaScript/Java/C++ guy, rather than PHP, but he suggests that features that look like bugs, or side effects which are indistinguishable from bugs, should be avoided for your sanity.
有些人更喜欢include,因为他们想使用那个“功能”。
Douglas Crockford 是一个 JavaScript/Java/C++ 人,而不是 PHP,但他建议应该避免看起来像错误的功能,或者与错误无法区分的副作用,为了您的理智。
Mind you, if your entire project is completely class-based (or functional), and entirely modular, then you shouldn't have much use for include, aside from, say resetting values on a configuration-object at application initialization (including admin-override options or dev-debugging options) on an object that was already required.
请注意,如果您的整个项目完全基于类(或功能性)并且完全模块化,那么除了在应用程序初始化时重置配置对象上的值(包括管理覆盖选项或开发调试选项)已经是required的对象。
As you might guess, there are other ways of doing this.
您可能会猜到,还有其他方法可以做到这一点。
And these are just suggestions, but suggestions that come from people who are smarter than I am, with decades of experience in dozens of languages.
这些只是建议,但建议来自比我更聪明、拥有数十种语言数十年经验的人。
回答by Banago
include_oncewill throw a warning, butwill not stop PHP from executing the rest of the script.
include_once将抛出警告,但不会阻止 PHP 执行脚本的其余部分。
require_oncewill throw an errorandwill stop PHP from executing the rest of the script.
require_once将抛出错误并阻止 PHP 执行脚本的其余部分。
回答by Amal Murali
The difference is that require_once()will produce a Fatal Error on failure, while include_once()only produces a warning.
不同之处在于require_once()失败时会产生致命错误,而include_once()只会产生警告。
回答by sabbir
page stop execute require_oncecan't be load required file and on the other hand include_oncewill continue instead of error in loading including file.
页面停止执行require_once无法加载所需的文件,另一方面include_once将继续而不是加载包含文件的错误。

