PHP include文件包含路径搜索问题的总结

黄舟
发布: 2017-06-25 10:59:42
原创
2628人浏览过

首先我们来看php官方手册中对include的文件搜索原则的描述:

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.
登录后复制

寻找包含文件的顺序先是在当前工作目录的相对的 include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 . ,当前工作目录是  ,脚本中要 include 一个   并且在该文件中有一句 include "b.php" ,则寻找   的顺序先是  ,然后是  。如果文件名以 ./  或者 ../  开始,则只在当前工作目录相对的 include_path 下寻找。

所以如下所示的文件结构

----a.php

----include/b.php

----include/c.php

 其中a.php

<?php
include 'include/b.php';
?>
-----------------------
b.php
<?php
include 'c.php';
include 'include/c.php';
?>
登录后复制

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

c.php

<?php
echo 'c.php';
?>
登录后复制

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

都能正确运行,说明b.php中两种不同包含路径都是可行的,根据include寻找包含文件的方式都能找到c.php。

但是最好的方式还是使用绝对路径,如果使用了绝对路径,php内核就直接通过路径去载入文件而不用去include path逐个搜索文件,增加了代码执行效率

<?php
define('ROOT_PATH',dirname(FILE));
include ROOT_PATH.'/c.php';
?>
登录后复制

不同的文件包含方式,程序的执行性能比较具体可以参照此文

<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>
登录后复制

以上就是PHP include文件包含路径搜索问题的总结的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号