Home >Backend Development >PHP Tutorial >Summary of PHP include file include path search issues
First of all, let’s look at the description of the file search principles of include in the official PHP manual:
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries , current working directory is , you included and there is include "b.php" in that file, is first looked in and then in . If filename begins with ./ or ../ , it is looked only in the current working directory.
The order to find include files is first in the current working directory. Search under the relative include_path, and then search under the include_path relative to the directory where the currently running script is located. For example, include_path is . , the current working directory is , and there is an include in the script and there is a sentence include "b.php" in the file, then the order of searching for is first and then . If the file name starts with ./ or ../, it will only be searched under the include_path relative to the current working directory.
So the file structure is as follows
----a.php
----include/b.php
----include/c.php
where a.php
<?php include 'include/b.php'; ?> ----------------------- b.php <?php include 'c.php'; include 'include/c.php'; ?>
------------------------ --
c.php
<?php echo 'c.php'; ?>
--------------------------
Both can run correctly, indicating that the two different include paths in b.php are feasible, and c.php can be found according to the include file search method.
But the best way is to use an absolute path. If an absolute path is used, the php kernel will load the file directly through the path without having to search for files one by one in the include path, which increases the code execution efficiency
<?php define('ROOT_PATH',dirname(FILE)); include ROOT_PATH.'/c.php'; ?>
Different file inclusion methods, please refer to this article for more details on program execution performance
<script type="text/ javascript "><!-- google_ad_client = "ca-pub-1944176156128447"; /* cnblogs 首页横幅 */ google_ad_slot = "5419468456"; google_ad_width = 728; google_ad_height = 90; //--></script> <script type="text/javascript" src=" </script>
The above is the detailed content of Summary of PHP include file include path search issues. For more information, please follow other related articles on the PHP Chinese website!