开发者

Problem with include in PHP

开发者 https://www.devze.com 2023-01-19 02:51 出处:网络
This works : require_once \'ModuleTest.php\'; But this won\'t : require_once \'./ModuleTest.php\'; Why ? T开发者_StackOverflow社区his leads to the same file ! (I am on Os X, so the file path is

This works :

require_once 'ModuleTest.php';

But this won't :

require_once './ModuleTest.php';

Why ? T开发者_StackOverflow社区his leads to the same file ! (I am on Os X, so the file path is correct). 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.

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');, it will look in more locations for it than if you say include('./ModuleTest.php');.

See the documentation page for include() to see exactly what happens.

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号