1

I have a MainClass.php file with other .php files in the same directory. I noticed that I can include other .php files inside this file by using:

require "file.php";

and it seems to load them even if I don't specify the full path to the script. Will this work on any server setup? Or do I have to manually add the full path or change the include path:

set_include_path(dirname(__FILE__));

before the require statements

?

1 Answer 1

3

By default, PHP's include path includes .. This represents the current working directory (CWD) which is why your includes are working.

I think it best to play it safe and use an explicit include path or absolute paths wherever possible as indicated in your question.

A popular method is

set_include_path(implode(PATH_SEPARATOR, array(
    $applicationIncludePath,
    get_include_path()
));
Sign up to request clarification or add additional context in comments.

2 Comments

by default? does that mean that the server admin can change this setting?
@Emm That's correct. The include path does not need to include . and may be configured without it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.