Summary of PHP include file include path search issues

黄舟
Release: 2023-03-11 12:02:01
Original
2196 people have browsed it

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.
Copy after login

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 &#39;include/b.php&#39;;
?>
-----------------------
b.php
<?php
include &#39;c.php&#39;;
include &#39;include/c.php&#39;;
?>
Copy after login

------------------------ --

c.php

<?php
echo &#39;c.php&#39;;
?>
Copy after login

--------------------------

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(&#39;ROOT_PATH&#39;,dirname(FILE));
include ROOT_PATH.&#39;/c.php&#39;;
?>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!