简体   繁体   English

包含在PHP中的问题

[英]Problem with include in PHP

This works : 这有效:

require_once 'ModuleTest.php';

But this won't : 但这不会:

require_once './ModuleTest.php';

Why ? 为什么? This leads to the same file ! 这导致了同一个文件! (I am on Os X, so the file path is correct). (我在Os X上,所以文件路径是正确的)。 Both files are in the same directory. 两个文件都在同一目录中。

require (and include ) works differently if you specify a path and if you are already in an included file. 如果指定路径并且您已经在包含文件中,则require (和include )的工作方式不同。

Without the path specified, it looks firstly in the include path (ie a pre-set list of locations where include files may be), then in the path of the original script that was run, and if it doesn't find it there, it looks in the path of the current file (ie the one that is actually doing the include). 如果没有指定路径,它首先会查看包含路径(即包含文件的位置的预设列表),然后在运行的原始脚本的路径中,如果在那里找不到,它查找当前文件的路径(即实际执行包含的文件)。

However if you specify a path, it skips the first part, and only looks at the path relative to the original script. 但是,如果指定路径,则会跳过第一部分,并仅查看相对于原始脚本的路径。

Therefore, if you say include('ModuleTest.php'); 因此,如果你说include('ModuleTest.php'); , it will look in more locations for it than if you say include('./ModuleTest.php'); ,它会比你说include('./ModuleTest.php');更多地寻找它include('./ModuleTest.php'); .

See the documentation page for include() to see exactly what happens. 请参阅include()文档页面,以确切了解发生的情况。

It can be confusing, and the best advice is to keep your path structure as easy to manage as possible. 它可能令人困惑,最好的建议是尽可能地保持您的路径结构易于管理。

Did you switch the slash to a '\\'? 你是否将斜线切换为'\\'? I think that may be the issue, but not being a OS X user, I am unsure... 我认为这可能是问题,但不是OS X用户,我不确定......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM