45IT.COM- 电脑学习从此开始!
DIY硬件教程攒机经验装机配置
设计Photoshop网页设计特效
系统注册表DOS系统命令其它
存储主板显卡外设键鼠内存
维修显卡CPU内存打印机
WinXPVistaWin7unix/linux
CPU光驱电源/散热显示器其它
修技主板硬盘键鼠显示器光驱
办公ExcelWordPowerPointWPS
编程数据库CSS脚本PHP
网络局域网QQ服务器
软件网络系统图像安全
页面导航: 首页 > 设计学院 > 网络编程 > PHP教程 >

include(“./file.php”)和include(“file.php”)区别

电脑软硬件应用网 45IT.COM 时间:2012-03-06 17:43 作者:佚名

多数情况下,两种方式的区别可能在性能上有细微的区别(via)。 但是,在多重包含的情况下表现未必一样:

|-- index.php
`-- lib
    |-- a.php
    `-- b.php假定有三个文件,入口文件index.php,包含lib/a.php。a.php又需要包含它同目录下的b.php:

方式1:include(“./b.php”) – 不工作

方式2:include(“b.php”) – 正常

我们通过strace来跟踪一下方式2的系统调用(我的环境下include_path=.:/tmp):
open("/home/robin/devel/lib/a.php", O_RDONLY) = 3
open("/home/robin/devel/b.php", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/tmp/b.php", O_RDONLY)            = -1 ENOENT (No such file or directory)
open("/home/robin/devel/lib/b.php", O_RDONLY) = 3官方文档的阐述是这样的:
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn’t found in the include_path, include() will finally check in the calling script’s own directory and the current working directory before failing.

所以优先级应该是这样:

有指定路径的话(如:”./”或者是”../”或”/home/…”),将直接根据路径来查找文件;

没有指定路径的情况

遍历include_path查找,include_path中有“.”开头的话,则指的是CWD(Current working directory,即是例中的index.php所在的目录)

在calling script(也就是例中的a.php)所在的目录查找

在CWD目录查找 – 不过在测试的时候发现似乎没有做到这一样。很容易测试:把include_path设成”/”避免其中有“.”,也就避免根据include_path方式查找到文件,再把b.php移到index.php同一级目录,结果包含不到。

当然,比较推荐结合魔术常量__FILE__来include。

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
无法在这个位置找到: baidushare.htm
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐知识