简体   繁体   中英

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><?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';?>

Now, the problem is, the second file doesn't show up in the table. First file works.

Php files work independently. No problem with the code.

I removed the include in 1.php and everything worked fine - ie. both the files show up in table.

My conclusion is, it goes on including indefinitely. Now, how do I solve this?

regards Ganesh Kumar

You can use 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.

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('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('login.php')

I've never found a reason to use this statement over 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++.

<?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. 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.


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. This method prevents that.

That said, I still use a require_once when necessary, and a require if it can be included multiple times. (Files with that designation are not designed with the define construct.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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