require、include、require_once 和 include_once 之间的区别?
在 PHP 中:
- 什么时候应该使用
require
和include
? - 什么时候应该使用
require_once
和include_once
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 PHP 中:
require
和 include
?require_once
和 include_once
?由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(28)
还有
require
和include_once
。所以你的问题应该是......
require
与include
?require_once
与require
1 的答案已描述 这里。
2 的答案可以在此处找到。
请注意,现在 *_once 变体几乎没有用处。如果您正在使用它们,这可能是一种盲目的习惯,或者您的代码需要重组。
There are
require
andinclude_once
as well.So your question should be...
require
vs.include
?require_once
vs.require
The answer to 1 is described here.
The answer to 2 can be found here.
Note that nowadays there is little use for *_once variants. If you are using them, it's likely either a mindless habit or your code needs to be restructured.
使用
require
当您的应用程序需要该文件时,例如重要的消息模板或包含配置变量的文件,如果没有这些文件,应用程序就会崩溃。
require_once
当文件包含会在后续包含时产生错误的内容时,例如
function important() { /* important code */}
在您的应用程序中肯定需要,但由于函数无法重新声明,因此不应再次包含。包含
当不需要文件并且应用程序流程在未找到时应继续时,例如
非常适合引用当前范围内的变量或其他内容的模板
include_once
可选的依赖项会在后续加载时产生错误,或者可能会导致远程文件包含错误,由于 HTTP 开销,您不希望发生两次错误
但基本上,何时使用哪个取决于您。
Use
require
when the file is required by your application, e.g. an important message template or a file containing configuration variables without which the app would break.
require_once
when the file contains content that would produce an error on subsequent inclusion, e.g.
function important() { /* important code */}
is definitely needed in your application but since functions cannot be redeclared should not be included again.include
when the file is not required and application flow should continue when not found, e.g.
great for templates referencing variables from the current scope or something
include_once
optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead
But basically, it's up to you when to use which.
我的建议是 99.9% 的时间只使用
require_once
。使用
require
或include
意味着您的代码在其他地方不可重用,即您拉入的脚本实际上是执行代码,而不是可用一个类或一些函数库。如果您需要/包括当场执行的代码,那就是过程代码,您需要了解新的范例。就像面向对象的编程、基于函数的编程或函数式编程。
如果您已经在进行面向对象或函数式编程,那么使用
include_once
基本上会延迟您在堆栈中发现错误/错误的位置。您是否想知道函数do_cool_stuff()
在您稍后调用它时不可用,或者通过要求 您希望它可用的那一刻图书馆?一般来说,最好立即知道您需要和期望的东西是否不可用,因此只需使用require_once
即可。或者,在现代 OOP 中,只需在使用时自动加载您的类。
My suggestion is to just use
require_once
99.9% of the time.Using
require
orinclude
instead implies that your code is not reusable elsewhere, i.e. that the scripts you're pulling in actually execute code instead of making available a class or some function libraries.If you are require/including code that executes on the spot, that's procedural code, and you need to get to know a new paradigm. Like object oriented programming, function-based programming, or functional programming.
If you're already doing OO or functional programming, using
include_once
is mostly going to be delaying where in the stack you find bugs/errors. Do you want to know that the functiondo_cool_stuff()
is not available when you go to call for it later, or the moment that you expect it to be available by requiring the library? Generally, it's best to know immediately if something you need and expect isn't available, so just userequire_once
.Alternatively, in modern OOP, just autoload your classes upon use.
带 _once 函数和不带 _once 函数的区别:
没有 _once 的代码将再次被包含,而使用 _once 函数的 PHP 会跟踪包含的文件并且只会包含它一次。
require 和 include 之间的区别:
如果未找到所需的文件,PHP 将发出致命错误,而对于 include 仅发出警告。
Difference between _once functions and without _once functions:
without _once code will be included again whereas with _once functions PHP keeps track of the included files and will include it only once.
Difference between require and include:
If a required file is not found PHP will emit a fatal error whereas for include only a warning will be emitted.
如果
include()
无法包含该文件,则会抛出警告,但脚本的其余部分将运行。如果脚本无法包含该文件,
require()
将抛出E_COMPILE_ERROR
并停止脚本。如果已经包含该文件,则
include_once()
和require_once()
函数不会再次包含该文件。请参阅以下文档页面:
include()
will throw a warning if it can't include the file, but the rest of the script will run.require()
will throw anE_COMPILE_ERROR
and halt the script if it can't include the file.The
include_once()
andrequire_once()
functions will not include the file a second time if it has already been included.See the following documentation pages:
您应该将类和函数定义组织在文件中。
使用
require_once()
加载依赖项(类、函数、常量)。使用
require()
加载类似模板的文件。使用
include_once()
加载可选依赖项(类、函数、常量)。使用
include()
加载可选的类似模板的文件。You should keep class and function definitions organized in files.
Use
require_once()
to load dependencies (classes, functions, constants).Use
require()
to load template-like files.Use
include_once()
to load optional dependencies (classes, functions, constants).Use
include()
to load optional template-like files.2018年7年后的答案
这个问题是七年前提出的,并且没有一个答案对该问题提供实际帮助。在现代 PHP 编程中,您主要只使用一次
require_once
来包含您的自动加载器类(通常为 Composer 自动加载器),它将加载您的所有类和函数(函数文件需要显式添加到 < code>composer.json 文件可在所有其他文件中使用)。如果您的类无法从自动加载器加载,您可以使用 require_once 来加载它。有些场合我们需要使用
require
。例如,如果您有一个非常大的数组定义,并且不想将其添加到类定义源代码中,您可以:如果您打算包含的文件包含可执行文件或声明一些变量,则几乎总是需要使用
require
,因为如果你除了第一个地方使用require_once
,你的代码将不会被执行和/或你的变量不会默默地启动,从而导致绝对难以修复的错误查明。include
和include_once
确实没有实际用例。An answer after 7 years for 2018
This question was asked seven years ago, and none of the answers provided practical help for the question. In modern PHP programming, you mainly use
require_once
only once to include your autoloader class (composer autoloader often), and it will load all of your classes and functions (functions files need to be explicitly added to thecomposer.json
file to be available in all other files). If your class is not loadable from autoloader you userequire_once
to load it.There are some occasions that we need to use
require
. For example, if you have a really big array definition and you don't want to add this to your class definition source code you can:If the file that you intend to include contains something executable or declares some variables, you almost always need to use
require
, because if you userequire_once
apart from the first place your code will not be executed and/or your variables will not initiate silently, causing bugs that are absolutely hard to pinpoint.There is no practical use case for
include
andinclude_once
really.每当您使用
require_once()
时,当您在当前文件中仅需要一次调用的文件时,可以在文件中使用 require_once() 来包含另一个文件。在示例中我有一个 test1.php。
在另一个我命名为 test2.php 的文件中,
您正在观看 m 两次需要 test1 文件,但该文件将包含 test1 一次,并且在第二次调用时,这将被忽略。并且无需停止就会显示一次输出。
每当您使用“include_once()”时,当您在当前文件中多次需要调用的文件时,可以在文件中使用“include_once()”来包含另一个文件。
在示例中,我有一个名为 test3.php 的文件。
在我命名为 test4.php 的另一个文件中,
当您正在观看包含 test3 文件的 m 时,将包含该文件一次,但会停止进一步执行。
Whenever you are using
require_once()
can be use in a file to include another file when you need the called file only a single time in the current file.Here in the example I have an test1.php.
and in another file that I have named test2.php
as you are watching the m requiring the the test1 file twice but the file will include the test1 once and for calling at the second time this will be ignored. And without halting will display the output a single time.
Whenever you are using 'include_once()` can be used in a file to include another file when you need the called file more than once in the current file.
Here in the example I have a file named test3.php.
And in another file that I have named test4.php
as you are watching the m including the test3 file will include the file a single time but halt the further execution.
对可重用的 PHP 模板使用“include”。对所需的库使用“require”。
“*_once”很好,因为它检查文件是否已经加载,但它只对我在“require_once”中有意义。
Use "include" for reusable PHP templates. Use "require" for required libraries.
"*_once" is nice, because it checks whether the file is already loaded or not, but it only makes sense for me in "require_once".
区别在于命令生成的错误。使用
require
,您想要使用的文件确实是必需的,因此如果未找到该文件,则会生成E_ERROR
。include
仅在失败时生成E_WARNING
错误,该错误或多或少是无声的。因此,如果需要该文件才能使其余代码正常工作,并且您希望脚本失败且文件不可用,请使用它。
对于
*_once()
:当然,这同样适用于
require_once()
。参考:
require()
,include_once()
The difference is in the error the commands generate. With
require
, the file you want to use is really required and thus generates anE_ERROR
if it is not found.include
only generates anE_WARNING
error if it fails which is more or less silent.So use it if the file is required to make the remaining code work and you want the script to fail the file is not available.
For
*_once()
:Same applies to
require_once()
of course.Reference:
require()
,include_once()
什么时候应该使用
require
或include
?require
和include
函数执行相同的任务,即包含并评估指定的文件,但不同的是require
会导致当指定的文件位置无效或出现任何错误时会出现致命错误,而include
将生成警告并继续执行代码。因此,如果您尝试包含的文件是系统的核心,并且可能会对其余部分造成巨大影响,则可以使用
require
函数当您尝试包含的文件是包含一些不太重要的代码的简单文件时,您可以使用include
函数。我个人的建议(对于不太重要的代码)是在开发阶段的代码中到处使用
require
函数,这样您就可以调试代码,然后替换所有include 函数>require 函数,这样,如果您错过任何错误,它不会影响最终用户,并且代码的其余部分可以正确执行...
何时应该使用
require_once
或require
?require
和require_once
之间的基本区别是require_once
会检查文件是否已经包含,如果已经包含则它不会包含该文件,而require
函数将包含该文件,无论文件是否已包含。因此,如果您想一次又一次地包含某些代码片段,请使用
require
函数,而如果您只想在代码中包含某些代码一次,请使用require_once
code>.When should one use
require
orinclude
?The
require
andinclude
functions do the same task, i.e. includes and evaluates the specified file, but the difference isrequire
will cause a fatal error when the specified file location is invalid or for any error whereasinclude
will generate a warning and continue the code execution.So you may use the
require
function in the case where the file you are trying to include is the heart of the system and may cause a huge impact on rest of the code and you can use theinclude
function when the file you are trying to include is a simple file containing some less important code.And my personal recommendation (for less important code) is to go for the
require
function everywhere in your code while it is in development phase such that you can debug code and later on replace allrequire
functions byinclude
function before moving it to production such that if you miss any bugs it will not affect the end user and the rest of the code executes properly...When should one use
require_once
orrequire
?The basic difference between
require
andrequire_once
isrequire_once
will check whether the file is already included or not if it is already included then it won't include the file whereas therequire
function will include the file irrespective of whether file is already included or not.So in cases where you want to include some piece of code again and again use the
require
function whereas if you want to include some code only once in your code, userequire_once
.1 - 如果文件不存在,“require”和“require_once”会抛出致命错误
现有并停止脚本执行
2 - “include”和“include_once”抛出警告并执行
继续
3 - 正如其名称所示,“require_once”和“include_once”,
如果该文件已包含在其中,则他们不会包含该文件
“需要”、“require_once”、“包含”或“include_once”
1 - "require" and "require_once" throw a fatal error if the file is not
existing and stop the script execution
2 - "include" and "include_once" throw a warning and the execution
continues
3 - "require_once" and "include_once" as their names suggests ,
they will not include the file if the file was already included with
"require", "require_once", "include" or "include_once"
要求文件必须存在,如果不存在,则会显示错误;而使用 include - 如果文件不存在,则页面将继续加载。
With require the file must exist, if it doesn't then an error will display; whereas with include - if the file doesn't exist then then the page will continue loading.
需要关键部分,例如授权并包括所有其他部分。
多重包含是非常糟糕的设计,必须完全避免。所以, *_once 并不重要。
Require critical parts, like authorization and include all others.
Multiple includes are just very bad design and must be avoided at all. So, *_once doesn't really matter.
包含 / 需要< /a> 您也可以多次包含同一文件:
require_once / include_once< /a>
Include / Require you can include the same file more than once also:
require_once / include_once
我使用的函数如下:
xyz.php 中声明了常量值。
我必须从另一个 PHP 脚本文件调用这个 doSomething() 函数。
但是我在循环中调用此函数时观察到行为,第一次迭代 doSomething() 在 xyz.php 中获取常量值,但后来每次迭代 doSomething() 都没有获取常量值能够获取
xyz.php
中声明的常量值。我通过从
require_once()
切换到include()
解决了我的问题,更新的doSomething()
代码如下:现在每次迭代调用
doSomething()
获取在xyz.php
中定义的常量值。I was using function as below:
There were constant values declared in xyz.php.
I have to call this doSomething() function from another PHP script file.
But I observed behavior while calling this function in a loop, for first iteration doSomething() was getting constant values within
xyz.php
, but later every iterationdoSomething()
was not able to get the constant values declared inxyz.php
.I solved my problem by switching from
require_once()
toinclude()
, updateddoSomething()
code is as below:Now every iteration call to
doSomething()
gets the constant values defined inxyz.php
.来自手册:
_once()
变体也是如此。From the manual:
The same is true for the
_once()
variants.include()
在找不到文件时会生成警告,但require_once()
将生成致命错误。另一件事是之前是否包含文件。然后
require_once()
将不会再次包含它。include()
will generate a warning when it doesn't find a file, butrequire_once()
will generate a fatal error.Another thing is if file is included before. Then
require_once()
will not include it again.在 PSR-0 / PSR-4 自动加载器时代,如果您只需要使某些函数/类可供您的代码使用,则可能完全没有必要使用任何语句(当然,如果您仍然使用 PHP 作为模板引擎,您仍然需要在引导文件中自动加载
require_once
本身并include
模板。In the age of
PSR-0 / PSR-4
autoloaders, it may be completely unnecessary to use any of the statements if all you need is to make some functions / classes available to your code (of course, you still need torequire_once
autoloader itself in your bootstrap file andinclude
templates if you still use PHP as a template engine).当你想加载模板样式的文件时使用include函数
如果你仍然困惑,只需始终使用 require_once 即可。
Use require function when you need to load any class, function, or dependency.
Use include function when you want to load template-styled file
If you are still confused, just use require_once for all time.
基本上,如果您需要错误的路径,PHP 会抛出致命错误并调用关闭函数,但是当您包含错误的路径时,PHP 将继续执行,但只会显示文件不存在的警告。
从英文单词 require 可以得知,PHP 页面或文件的执行取决于所需的文件。
根据我的经验,需要重要文件(例如配置文件、数据库类和其他重要实用程序)是很正常的。
Basically, if you require a wrong path, PHP throws a fatal error and the shutdown function is called, but when you include a wrong path, PHP will continue execution, but it will just display a warning that the file does not exist.
From the English word require, PHP is told that the execution of the page or file depends on the file required.
From my experience, it's norm to require important files such as configuration files, database classes and other important utilities.
它们都是包含文件的方式。
需要意味着它需要它。 Require_once 意味着它将需要它,但只需要它一次。包含意味着它将包含一个文件,但不需要它才能继续。
示例:
还有一个 include_once 函数,它包含一次文件。
当我用手机打字时,不要使用大写字母。
They are all ways of including files.
Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn’t need it to continue.
Examples:
There is also an include_once function which includes a file once.
Don’t use capital letters where I have as I am typing from my phone.
我注意到的一件事是,当使用 include 时,我只能从包含它的文件中访问包含的文件函数。使用 require_once,我可以在第二个 required_once 文件中运行该函数。
另外:我建议添加
因为当 require_once 杀死页面时,它有时会回显网站文件的目录
这是我为 require 文件制作的自定义函数:
用法示例:
one thing I noticed, when using include I can only access the included files functions from the file that included it. With require_once, I can run that function in a second required_once file.
also: I recommend adding
Because when require_once kills the page, it can sometimes echo the directory of your website files
Here's a custom function I made to require files:
usage example:
require
生成致命错误,并在找不到文件时停止下一行执行。include
生成警告,但在找不到文件时没有停止下一行执行。require_once
与require
执行相同的操作,但它会检查文件是否已经加载或不执行。include_once
与include
的作用相同,但它会检查文件是否已经加载或不执行。注意:在脚本的特定执行期间可能多次包含和评估同一文件的情况下,可以使用
include_once
或require_once
,因此在这种情况下可能会有助于避免函数重定义、变量值重新赋值等问题。require
generate fatal error with stopping next line execution while file not found.include
generate warning but did not stop next line execution while file not found.require_once
do the same asrequire
do but it will check if the file has already been loaded or not to be executed.include_once
do the same asinclude
do but it will check if the file has already been loaded or not to be executed.Note:
include_once
orrequire_once
may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.在 PHP 中,有 4 个类似的函数用于包含和请求文件:
include()
:include()
语句用于将一个文件的内容包含到另一个文件中。如果找不到包含的文件,将生成警告,但脚本将继续执行。require()
:require()
语句与 include 类似,但如果找不到文件,它会生成致命错误。该脚本将立即停止执行。include_once()
:include_once()
语句与 include 相同,但它只会包含文件一次,即使在同一脚本中包含多次。require_once()
:require_once()
语句与 require 相同,但它只会包含该文件一次,即使它在同一脚本中包含多次。简单来说:
当您想要包含文件并继续执行脚本(即使文件丢失)时,请使用
include()
。当您需要包含文件并在文件丢失时停止执行脚本时,请使用
require()
。当您只想包含一个文件一次以避免重复函数或变量出现任何问题时,请使用
include_once()
或require_once()
。注意:一般情况下,建议使用
require_once()
而不是include_once()
以确保所需文件包含且不丢失。In PHP, there are 4 similar functions for including and requiring files:
include()
: Theinclude()
statement is used to include the contents of a file into another file. If the file being included cannot be found, a warning will be generated but the script will continue to execute.require()
: Therequire()
statement is similar to include, but it generates a fatal error if the file cannot be found. The script will stop executing immediately.include_once()
: Theinclude_once()
statement is the same as include, but it will only include the file once, even if it is included multiple times in the same script.require_once()
: Therequire_once()
statement is the same as require, but it will only include the file once, even if it is included multiple times in the same script.In simple terms:
Use
include()
when you want to include a file and continue executing the script even if the file is missing.Use
require()
when you need to include a file and stop executing the script if the file is missing.Use
include_once()
orrequire_once()
when you want to include a file only once to avoid any issues with duplicate functions or variables.Note: In general, it is recommended to use
require_once()
overinclude_once()
to ensure that the required file is included and not missing.这通常是一个问题,您是否想要有条件地加载客户端库,或者无论您是否要使用它,都继续加载它。
这是具体的例子;详细阐述 pcj 所说的话。
假设您有一个存储数据库用户名和密码的配置文件(conf.php):
以及一个具有使用数据库的静态函数的类:
并且该静态函数在循环内迭代调用的另一个函数内部使用:
您只能要求/包含该类一次。如果您在循环的每次迭代中都需要/包含它,则会收到错误。但是,每次调用静态函数时都必须包含conf 文件。
当然,将其移到函数之外可能是该问题的解决方案:
除非您担心加载可能仅在某些条件下使用的类的开销,并且不想在不使用时加载它。
It's often a matter of whether you want to load a client library conditionally, or go ahead and load it whether or not you're going to use it.
Here's concrete example; elaborating on what pcj said.
Say you have a configuration file storing your database username and password (conf.php):
And a class with a static function that uses the database:
And that static function is used inside of another function that is being called iteratively inside a loop:
You can only require/include the class once. If you require/include it on every iteration of your loop, you'll get an error. However, you have to include your conf file every time the static function is called.
Of course, moving it outside of the function could be a solution to that problem:
Unless you're concerned with the overhead of loading a class that might only be used in certain conditions and don't want to load it when it isn't.
require
的开销比include
更大,因为它必须首先解析文件。将requires
替换为includes
通常是一种很好的优化技术。require
has greater overhead thaninclude
, since it has to parse the file first. Replacingrequires
withincludes
is often a good optimization technique.只需使用 require 和 include 即可。
因为思考如何使用 include_once 或 require_once 。
即寻找保存包含或所需 PHP 文件的日志数据。
所以这比 include 和 require 慢。
就这样使用...
Just use require and include.
Because think how to work with include_once or require_once.
That is looking for log data which save included or required PHP files.
So that is slower than include and require.
Just using like this...