php 包括、include_once、require 或 require_once?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3546160/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:10:22  来源:igfitidea点击:

include, include_once, require or require_once?

phpinclude

提问by Ibrahim Azhar Armar

I have PHP file where I have defined the server access variables as well as the mysql_connectand mysql_select_db, as this functions are regularly used in almost every page in backend, while I am using include()which is perfectly working for me now, which method or function would you suggest and I would like to know if there is any flaw if I use include()or is it safe to use it?

我有 PHP 文件,我在其中定义了服务器访问变量以及mysql_connectand mysql_select_db,因为这些函数在后端的几乎每个页面中都经常使用,而我正在使用include()它现在对我来说非常有用,你会建议哪种方法或函数我想知道如果我使用include()它是否有任何缺陷或者使用它是否安全?

Edit : Keeping in mind I'll be using $_SESSIONvariable too.

编辑:记住我也会使用$_SESSION变量。

回答by Sasha Chedygov

The only difference between the two is that requireand its sister require_oncethrow a fatal error if the file is not found, whereas includeand include_onceonly show a warning and continue to load the rest of the page. If you don't want PHP to attempt to load the rest of your page without the database info (which I would assume), then use require_once. You don't need to include the file more than once, so there is no need to use the regular requirefunction.

两者之间的唯一区别是,require和它的姐妹require_once抛出致命错误,如果没有找到文件,而includeinclude_once只显示一个警告并继续加载页面的其余部分。如果您不希望 PHP 在没有数据库信息的情况下尝试加载页面的其余部分(我认为),那么使用require_once. 您不需要多次包含该文件,因此无需使用常规require函数。

回答by ReadMyViews Com

Functional Work :All functions perform similar work. All functions will include and evaluates the specific file while executing the code.

功能性工作:所有功能都执行相似的工作。所有函数都将在执行代码时包含并评估特定文件。

Functional Difference :

功能差异:

include vs include_once :There is only one difference between include() and include_once(). If the code from a file has been already included then it will not be included again if we use include_once(). Means include_once() include the file only once at a time.

include vs include_once :include() 和 include_once() 之间只有一个区别。如果文件中的代码已经被包含,那么如果我们使用 include_once() 就不会再次包含它。意味着 include_once() 一次只包含文件一次。

include vs require :if include() is not able to find a specified file on location at that time it will throw a warning however, it will not stop script execution. For the same scenario, require() will throw a fatal error and it will stop the script execution.

include vs require :如果 include() 无法在当时的位置找到指定的文件,它将抛出警告,但不会停止脚本执行。对于相同的场景,require() 将抛出一个致命错误并停止脚本执行。

require vs require_once :There is only one difference between require() and require_once(). If the code from a file has been already included then it will not be included again if we use require_once(). Means require_once() include the file only once at a time.

require 与 require_once :require() 和 require_once() 之间只有一个区别。如果文件中的代码已经被包含,那么如果我们使用 require_once() 就不会再次包含它。意味着 require_once() 一次只包含文件一次。

To get the detailed knowledge with example please review these amazing articles
(1) http://www.readmyviews.com/include-vs-include-once/
(2) http://www.readmyviews.com/include-vs-require/

要通过示例获得详细知识,请查看这些精彩的文章
(1) http://www.readmyviews.com/include-vs-include-once/
(2) http://www.readmyviews.com/include-vs-要求/

回答by Ashay

For the database connection variables, use of require_once() function will be preferable. If the connection fails for any reason you can show the failure message.

对于数据库连接变量,最好使用 require_once() 函数。如果连接因任何原因失败,您可以显示失败消息。

回答by wimvds

If your page will not work without the DB connection, then require_once would be the only correct option (since you don't want to load these settings twice, loading them once should suffice). Include will try to load your page even if the settings file is not available.

如果您的页面在没有数据库连接的情况下无法工作,那么 require_once 将是唯一正确的选项(因为您不想加载这些设置两次,加载一次就足够了)。即使设置文件不可用,包含也会尝试加载您的页面。

回答by Hoshang Karanjekar

<?php

include('db.php');   

echo "<br>"."Included"."<br>";

include_once('db.php');

echo "<br>"."Again included"."<br>";

?>

In the Above Code, I have included a file using include statement at the top, the file get included.

在上面的代码中,我在顶部使用 include 语句包含了一个文件,该文件被包含在内。

Next I have used include_once to include the same file, But as the file was already included above, it will not be included again here.

接下来我使用 include_once 来包含同一个文件,但是由于上面已经包含了该文件,所以这里不会再包含它了。

Output:

输出:

Connected             -----This is from db.php File
Included

Again included

==========================

==========================

include_once('db.php');

echo "<br>"."Again included"."<br>";

include('db.php');

echo "<br>"."Included"."<br>";

?>

In the above code, I have used include_once at the top, so the file is included But in the next code I have again used include_once for the same file, then again the file will get included and the output will be

在上面的代码中,我在顶部使用了 include_once,所以文件被包含但在下一个代码中,我再次对同一个文件使用了 include_once,然后文件将再次被包含,输出将是

Output:

输出:

Again included
Connected
Included
Connected

回答by Affan

Include The include() statement includes and evaluates the specified file.

Include include() 语句包括并评估指定的文件。

Include Once The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

包含一次 include_once() 语句在脚本执行期间包含并评估指定的文件。这是一种类似于 include() 语句的行为,唯一的区别是如果来自文件的代码已经被包含,则不会再次被包含。顾名思义,它将只包含一次。

Require require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page.

Require require() 和 include() 在各个方面都是相同的,除了它们如何处理失败。它们都会产生警告,但 require() 会导致致命错误。换句话说,如果您希望丢失的文件停止处理页面,请不要犹豫使用 require()。

Require Once The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again.

要求一次 require_once() 语句在脚本执行期间包括并评估指定的文件。这是一种类似于 require() 语句的行为,唯一的区别是如果来自文件的代码已经被包含,则不会再次被包含。

回答by Edd Turtle

You should use include_once() if you're including it more than once on a page.

如果您在一个页面上多次包含它,则应该使用 include_once()。

回答by Alex Pliutau

require() is better for you. Because with require file includes before script compilation. inluce() using in dinamical including.

require() 更适合你。因为在脚本编译之前使用 require 文件包含。inluce() 在动态包括中使用。