当文件存在于路径和本地目录中时,将包含哪个文件?
如果我的路径中有一个名为 inc.php 的文件,而另一个名为 inc.php 的文件位于与 index.php 相同的目录中,并且在 index.php 中我放置了
include('inc.php')
哪个文件将被包含?
If I have a file in the path called inc.php and another file called inc.php in the same directory as index.php, and in index.php I place
include('inc.php')
Which file will be included?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您执行包含时,PHP 会搜索包含路径中的每个条目,检查文件是否存在,直到找到它为止,然后将来自该条目的文件包含在包含路径中。
通常,包含路径具有 . (当前工作目录)作为其第一个条目。
因此,除非您更改了 include_path,以便 .不再是第一个条目,它将包含当前工作目录中的文件因为它也是在 include_path 中找到的文件的第一个副本。
When you do an include, PHP searches through each entry in the include path checking for the existence of the file until it finds it, whereupon it includes the file from that entry in the include path.
Typically, the include path has . (the current working directory) as its first entry.
So unless you've changed the include_path so that . is no longer the first entry, it will include the file from the current working directory because it's also the first copy of the file that it finds in the include_path.
PHP 将从左到右遍历您的
包含路径
,并针对每个文件夹检查该文件夹中是否存在这样命名的文件。它将包括它找到的第一个。
如果您包含的路径类似于
".:/some/dir/:/some/other/dir"
,它将首先在当前文件夹中查找。然后检查其他的,如果没有发现任何东西。因此通常它会将文件包含在同一文件夹中。
要查看包含路径,请使用 (fe)
echo ini_get("include_path");
PHP will go over your
include path
from left to right and for every folder check if a file named like this exists in that folder.It will include the first one it finds.
If you include path looks like
".:/some/dir/:/some/other/dir"
it will look in the current folder first. Then check the others if it doesn't find anything.So usually it will include the file in the same folder.
To see what your include path looks like use (f.e.)
echo ini_get("include_path");
在
include()
函数中可以找到文档:因此,首先搜索
include_path
然后搜索当前目录(但是如果里面有.
,你的include_path
可能会包含当前目录,甚至可能先搜索这取决于您的服务器配置)。As could be found in the
include()
function documentation :So the
include_path
is searched first then the current directory (But yourinclude_path
could include the current directory if there is a.
inside, maybe even first it depends on your server configuration).