包含外部 php 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14079459/
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
include external php files
提问by Pramod
I am using some php code like database connection which is common for all the pages, so I created a php file which contains the php code and then I am including this file in my HTML code, So I want to know the better way to include php file, better alternative for include function. my example code is here
我正在使用一些 php 代码,比如所有页面都通用的数据库连接,所以我创建了一个包含 php 代码的 php 文件,然后我在我的 HTML 代码中包含了这个文件,所以我想知道更好的包含方式php 文件,更好的替代包含功能。我的示例代码在这里
<?php
include "check_timeout.php";
include "connect_to_db.php";
?>
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
mysql_close($con);
?>
</body>
</html>
Thank you in advance.
先感谢您。
回答by Average Joe
You have 4 options to choose from.
您有 4 个选项可供选择。
include 'yourfile.php';
include_once 'yourfile.php';
require 'yourfile.php';
require_once 'yourfile.php';
包括'yourfile.php';
include_once '你的文件.php';
需要'yourfile.php';
require_once '你的文件.php';
of course you can also use " instead of '.
当然你也可以用“代替'。
they all will do the same thing except minor differences.
除了细微的差异外,他们都会做同样的事情。
if yourfile.php does not exist, the include ones will ignore that fact and your php page will move on - without any fatal errors.
如果 yourfile.php 不存在,则包含的将忽略该事实并且您的 php 页面将继续前进 - 没有任何致命错误。
require ones on the other hand will create fatal error.
另一方面,需要会产生致命错误。
if you know that that file is there, it makes no difference as to which one you pick.
如果您知道该文件在那里,则选择哪个文件没有任何区别。
As to the options with the _once postfix, well, they tend to be slower compared to their none _once postfixed counterparts. Cause when you use the include_once or the require_once, PHP will do some extra work to make sure that those files are truly included ONCE - protecting you from a possible double include situation if you carelessly code and many files use many includes and you may run into situations where the same file gets included twice. Well, _once option will prevent that. And that checking will come with some processing cost.
至于带有 _once 后缀的选项,好吧,与没有 _once 后缀的选项相比,它们往往更慢。因为当您使用 include_once 或 require_once 时,PHP 会做一些额外的工作以确保这些文件真正包含一次 - 如果您不小心编码并且许多文件使用许多包含,则保护您免受可能的双重包含情况,并且您可能会遇到同一文件被包含两次的情况。好吧, _once 选项将阻止这种情况。而且这种检查会带来一些处理成本。
I also noticed you've used " as opposed to ' for your file delimiters. There is no reason to choose " over ' unless you will be referring to variable names in your files such as
我还注意到您使用了 " 而不是 ' 作为文件分隔符。没有理由选择 " 而不是 ' 除非您将引用文件中的变量名,例如
$thefile = 'yourfile.php; include "$thefile";
$thefile = '你的文件.php; 包括“$thefile”;
so which ones to pick out of these 4? it all depends, if you think you do need to force the _once issue, then you pick either the include_once or require_once, and if you think that's not needed, then you go with the include or require. As to include vs require, it all comes down to would you like your PHP script die or move on if yourfile is for some reason not accessible.
那么从这 4 个中挑选哪些呢?这一切都取决于,如果您认为确实需要强制执行 _once 问题,那么您可以选择 include_once 或 require_once,如果您认为不需要,那么您可以使用 include 或 require。至于包含与要求,这一切都归结为如果您的文件由于某种原因无法访问,您希望您的 PHP 脚本死掉还是继续前进。
If you are curious about some speed tests, here is a link for you to check out. http://php.net/manual/en/function.require-once.php
如果您对某些速度测试感到好奇,这里有一个链接供您查看。 http://php.net/manual/en/function.require-once.php
I also found this on the subject matter.
我也在这个主题上找到了这个。
Understanding the difference between require and include According to the PHP manual, require and include "are identical in every way except how they handle failure." However, further reading of the manual suggests another very subtle difference that impacts performance. When you use the require keyword, the named file is read in, parsed, and compiled when the file using the require keyword is compiled. When a file containing the include keyword is compiled, the named file is not read in, parsed, and compiled initially. Only when that line of code is executed is the file read, parsed and compiled. Only use the require keyword if you know you will always need that named file in the current script. If you might use its functions, use include instead. PHP opens up all files that are required, but only opens included files as needed. Additionally, you should also consider using require_once and include_once in place of require and include respectively. In practice, it is more likely that you actually want the functionality provided by the require_once and include_once functions, even though it is much more common to use the require and include keywords respectively. Refer to the following PHP manual pages for more information: include, include_once
source: http://www.stevengould.org/portfolio/developerWorks/efficientPHP/wa-effphp/wa-effphp-a4.pdf
了解 require 和 include 之间的区别 根据 PHP 手册,require 和 include“除了处理失败的方式外,在各方面都相同”。但是,进一步阅读手册会发现另一个影响性能的非常细微的差异。使用 require 关键字时,在编译使用 require 关键字的文件时,会读入、解析和编译命名文件。编译包含 include 关键字的文件时,命名文件最初不会被读入、解析和编译。只有当那行代码被执行时,文件才会被读取、解析和编译。仅当您知道在当前脚本中始终需要该命名文件时才使用 require 关键字。如果您可能会使用它的功能,请改用 include。PHP 打开所有需要的文件,但仅根据需要打开包含的文件。此外,您还应该考虑使用 require_once 和 include_once 分别代替 require 和 include。在实践中,您更有可能真正想要 require_once 和 include_once 函数提供的功能,尽管分别使用 require 和 include 关键字更为常见。有关更多信息,请参阅以下 PHP 手册页:include、include_once
来源:http: //www.stevengould.org/portfolio/developerWorks/efficientPHP/wa-effphp/wa-effphp-a4.pdf
回答by ma?ek
Always use requireor require_onceas you will receive an E_COMPILE_ERRORin the event something is misconfigured.
始终使用require或在某些配置错误的情况下require_once您将收到E_COMPILE_ERROR。
In terms of performance, includeand requireare going to be functionally identical.
在性能方面,include并且require将在功能上相同。
The requireitself is only going to be as performant as the code that's contained within each file.
其require本身只会与每个文件中包含的代码一样高效。

