简体   繁体   English

如何包括已经包含另一个文件的php文件?

[英]how to include a php file that already includes another file?

I am trying to include 2 php file in two separate <td> tags in the same table. 我试图在同一张表的两个单独的<td>标签中包含2个php文件。

<td><?php include 'login.php';?> </td>
<td><?php include 'register.php';?> </td>

Both the php files include another php file for connecting to a database (eg. <?php include 'database.php';?> 这两个php文件都包含另一个用于连接数据库的php文件(例如<?php include 'database.php';?>

Now, the problem is, the second file doesn't show up in the table. 现在的问题是,第二个文件没有显示在表格中。 First file works. 第一个文件有效。

Php files work independently. php文件独立工作。 No problem with the code. 代码没问题。

I removed the include in 1.php and everything worked fine - ie. 我删除了1.php中的include,一切正常-即。 both the files show up in table. 这两个文件都显示在表中。

My conclusion is, it goes on including indefinitely. 我的结论是,它会无限期地继续下去。 Now, how do I solve this? 现在,我该如何解决?

regards Ganesh Kumar 问候Ganesh Kumar

You can use include-once 您可以使用一次包含

The include_once statement includes and evaluates the specified file during the execution of the script. include_once语句在脚本执行期间包含并评估指定的文件。 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 . 这是与include语句类似的行为,唯一的区别是,如果已经包含了文件中的代码, 则不会再次包含 As the name suggests, it will be included just once. 顾名思义,它将仅包含一次。

ie: 即:

 include_once('database.php');
 include_once('login.php');
 include_once('register.php');

You actually have several options, now that I think about it. 现在考虑一下,您实际上有几种选择。

Require_once: Require_once:

require_once('database.php')

This is the most accepted method for files such as this one that you describe, as it will hard-fail if the file cannot be included. 这是您所描述的诸如此类文件的最受接受的方法,因为如果无法包含该文件,它将很难通过。 For files that do program instantiation (Ie database connection) this method is preferred. 对于执行程序实例化(即数据库连接)的文件,此方法是首选。

Include_once: Include_once:

include_once('login.php')

I've never found a reason to use this statement over require_once ; 我从来没有找到在require_once上使用此语句的理由; however, that said, it doesn't mean there isn't one. 但是,那并不意味着就没有一个。 If you have a file that does some instantiation of something related to your programme that isn't mission-critical, then you could suppose to use this directive over the other. 如果您有一个文件对与程序无关的任务进行某些实例化,而这些任务并不是关键任务,那么您可以假设使用该指令来代替其他指令。

Define Include Constants: 定义包含常量:

This method requires a bit more explanation: instead of starting your included file ( database.php in our example) off with the code for it, start it off in a manner similar to C/C99/C++. 此方法需要更多解释:与其以包含其代码的方式启动包含的文件(在本示例中为database.php ),否则以类似于C / C99 / C ++的方式启动它。

<?php
    if (!defined("INCLUDED_DATABASE"))
    {
        define("INCLUDED_DATABASE", true);

        // add main body of file here
    }
?>

This method basically accomplishes the same thing as the include_once and require_once , except that in no circumstances will it ever actually process the body twice in one request, even if you forget to use _once as a suffice to your include / require method. 此方法基本上完成了与include_oncerequire_once相同的任务,除了在任何情况下 ,即使您忘记使用_once作为include / require方法的足够条件,它也不会在一个请求中实际对主体进行两次处理。 This goes back to the old days of C/C99/C++ where including a file twice would hard-fail the compiler, as duplicate definitions would take place. 这可以追溯到C / C99 / C ++的旧时代,在C / C99 / C ++中,两次包含一个文件会使编译器发生故障,因为会发生重复的定义。


Personally, I have always preferred the last option: it's the most strict. 就我个人而言,我总是更喜欢最后一个选择:这是最严格的。 Yes, require_once and include_once when used diligently will have the same effect, but suppose someone (not even you necessarily) is modifying the application and accidentally does an include or require without the _once suffix, they will be having a bad day. 是的,当勤奋地使用require_onceinclude_once会具有相同的效果,但是假设有人(甚至没有必要)正在修改应用程序,并且意外地在没有_once后缀的情况下进行了includerequire ,那么他们的日子将会很糟糕。 This method prevents that. 这种方法可以防止这种情况。

That said, I still use a require_once when necessary, and a require if it can be included multiple times. 就是说,我仍然在需要时使用require_once ,并且可以多次包含require (Files with that designation are not designed with the define construct.) (具有该名称的文件未使用define结构设计。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM