php require、include、require_once 和 include_once 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2418473/
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 require, include, require_once and include_once?
提问by Scott B
In PHP:
在 PHP 中:
- When should I use
requirevs.include? - When should I use
require_oncevs.include_once?
- 我什么时候应该使用
requirevs.include? - 我什么时候应该使用
require_oncevs.include_once?
回答by Leo
There are requireand include_onceas well.
有require和include_once也。
So your question should be...
所以你的问题应该是...
- When should I use
requirevs.include? - When should I use
require_oncevs.require
- 我什么时候应该使用
requirevs.include? - 我应该什么时候使用
require_oncevs.require
The answer to 1 is described here.
The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.
require() 函数与 include() 相同,只是它处理错误的方式不同。如果发生错误,include() 函数会生成警告,但脚本将继续执行。require() 生成一个致命错误,脚本将停止。
The answer to 2 can be found here.
可以在此处找到 2 的答案。
The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.
require_once() 语句与 require() 相同,除了 PHP 将检查文件是否已被包含,如果是,则不再包含(要求)它。
回答by Gordon
Use
用
require
when the file is requiredby 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.includewhen 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 somethinginclude_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
需要
时,该文件要求您的应用程序,例如,一个重要的信息模板或没有这些应用程序将打破一个包含文件中的配置变量。require_once
当文件包含会在后续包含时产生错误的内容时,例如function important() { /* important code */},在您的应用程序中肯定需要,但由于函数不能重新声明,因此不应再次包含。当不需要文件时包含,并且在找不到时应用程序流应该继续,例如
非常适合模板引用当前范围内的变量或其他东西include_once
可选依赖项会在后续加载时产生错误,或者可能由于 HTTP 开销而导致您不希望发生两次的远程文件包含
But basically, it's up to you when to use which.
但基本上,什么时候使用哪个取决于你。
回答by Kzqai
My suggestion is to just use require_once99.9% of the time.
我的建议是只使用require_once99.9% 的时间。
Using requireor includeinstead implies that your code is not reusable elsewhere, i.e. that the scripts you're pulling in actually executecode instead of making availablea class or some function libraries.
使用requireorinclude代替意味着您的代码不能在其他地方重用,即您引入的脚本实际上是执行代码,而不是使类或某些函数库可用。
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_onceis mostly going to be delayingwhere in the stack you find bugs/errors. Do you want to know that the function do_cool_stuff()is not available when you go to call for it later, or the moment that you expect it to be availableby requiring the library? Generally, it's best to know immediately if something you need and expect isn't available, so just use require_once.
如果您已经在进行 OO 或函数式编程,则使用include_once主要是延迟您在堆栈中发现错误/错误的位置。您是否想知道该函数do_cool_stuff()在您稍后调用它时不可用,还是通过要求该库而期望它可用的那一刻?一般来说,如果您需要和期望的东西不可用,最好立即知道,所以只需使用require_once.
Alternatively, in modern OOP, just autoloadyour classes upon use.
或者,在现代 OOP 中,只需在使用时自动加载您的类。
回答by Sebastian
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.
_once 函数和没有 _once 函数之间的区别:没有 _once 函数的代码将再次被包含,而带有 _once 函数的 PHP 会跟踪包含的文件,并且只会包含一次。
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.
require 和 include 之间的区别:如果没有找到所需的文件,PHP 将发出致命错误,而对于 include 只会发出警告。
回答by Polynomial
include()will throw a warning if it can't include the file, but the rest of the script will run.
include()如果它不能包含该文件,则会发出警告,但脚本的其余部分将运行。
require()will throw an E_COMPILE_ERRORand halt the script if it can't include the file.
require()E_COMPILE_ERROR如果脚本不能包含文件,则会抛出并停止脚本。
The include_once()and require_once()functions will not include the file a second time if it has already been included.
该include_once()和require_once()功能将不包括在文件中的第二次,如果已经被列入了。
See the following documentation pages:
请参阅以下文档页面:
回答by AliMohsin
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.
每当你使用require_once()可以在一个文件中使用,包括其他文件,当您需要调用的文件只能在当前文件一次。在示例中,我有一个 test1.php。
<?php
echo "today is:".date("Y-m-d");
?>
and in another file that I have named test2.php
在另一个我命名为 test2.php 的文件中
<?php
require_once('test1.php');
require_once('test1.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.
当您正在观看需要 test1 文件的 m 两次但该文件将包含一次 test1 并且第二次调用时,这将被忽略。并且不停机将显示输出一次。
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.
每当您使用 'include_once()` 时,当您需要在当前文件中多次调用被调用文件时,都可以在文件中使用'include_once()` 来包含另一个文件。在示例中,我有一个名为 test3.php 的文件。
<?php
echo "today is:".date("Y-m-d");
?>
And in another file that I have named test4.php
在另一个我命名为 test4.php 的文件中
<?php
include_once('test3.php');
include_once('test3.php');
?>
as you are watching the m including the test3 file will include the file a single time but halt the further execution.
当您正在观看包含 test3 文件的 m 时,将包含该文件一次,但会停止进一步执行。
回答by mo.
回答by Tiberiu-Ionu? Stan
You should keep class and function definitions organized in files.
您应该在文件中组织类和函数定义。
Use require_once()to load dependencies(classes, functions, constants).
使用require_once()负载依赖性(类,函数,常量)。
Use require()to load template-like files.
使用require()负载类似模板文件。
Use include_once()to load optional dependencies(classes, functions, constants).
使用include_once()加载可选依赖(类,函数,常量)。
Use include()to load optional template-like files.
使用include()加载可选的类似模板文件。
回答by PHPst
An answer after 7 years for 2018
2018年7年后的答案
This question was asked seven years ago, and none of answers provide practical help for the question. In the modern PHP programming you mainly use required_onceonly once to include your autoloader class (composer autoloader often), and it will loads all of your classes and functions (functions files need to be explicitly added to composer.jsonfile to be available in all other files). If for any reason your class is not loadable from autoloader you use require_onceto load it.
这个问题是七年前提出的,没有一个答案为这个问题提供实际帮助。在现代 PHP 编程中,您主要required_once只使用一次来包含您的自动加载器类(通常是 Composer 自动加载器),它将加载您的所有类和函数(函数文件需要显式添加到composer.json文件中才能在所有其他文件中可用)。如果由于任何原因您的类无法从require_once用于加载它的自动加载器加载。
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:
有些场合我们需要使用require. 例如,如果您有一个非常大的数组定义并且您不想将其添加到您的类定义源代码中,您可以:
<?php
// arr.php
return ['x'=>'y'];
<?php
//main.php
$arr= require 'arry.php'
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 use require_onceapart 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.
如果您打算包含的文件包含一些可执行文件或声明了一些您几乎总是需要使用的变量require,因为如果您require_once除了第一次使用之外,您的代码将不会被执行和/或您的变量不会静默启动,从而导致错误绝对难以确定。
There is no practical use case for includeand include_oncereally.
没有实际的使用情况include和include_once真。
回答by Felix Kling
The difference is in the error the commands generate. With require, the file you want to use is really required and thus generates an E_ERRORif it is not found.
不同之处在于命令生成的错误。使用require,您要使用的文件确实是必需的,因此E_ERROR如果找不到,则会生成一个。
require()is identical toinclude()except upon failure it will also produce a fatalE_ERRORlevel error.
require()与 相同,include()除了在失败时它也会产生致命的E_ERROR级别错误。
includeonly generates an E_WARNINGerror if it fails which is more or less silent.
include只有E_WARNING在失败时才会产生错误,这或多或少是无声的。
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():
对于*_once():
include_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.
include_once()可用于在脚本的特定执行期间可能包含和评估同一文件不止一次的情况,因此在这种情况下,它可能有助于避免诸如函数重新定义、变量值重新分配等问题。
Same applies to require_once()of course.
require_once()当然同样适用。
Reference: require(), include_once()

