简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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