0

I am trying to set things up so when we create a testing folder, I can easily change the path to an important file in various places. But, running into an issue.

$folder = "/test";

function foo(){
global $folder;
require_once($_SERVER['DOCUMENT_ROOT'].$folder."/order/includes/db.php");
..do stuff

}

Does not work

function foo(){
require_once($_SERVER['DOCUMENT_ROOT']."/test/includes/db.php");
..do stuff
}

Does work

What am I missing?

1
  • 1
    /order/ is missing in the second snippet (the working one) Commented Oct 14, 2015 at 18:48

1 Answer 1

1

Remove the global $folder; from your function foo() and it shall work. Another alternative is adding the keyword global to your declaration:

$folder = "/test";

function foo(){
    require_once($_SERVER['DOCUMENT_ROOT'].$folder."/order/includes/db.php");
    ..do stuff

}

OR

global $folder = "/test";

function foo(){
global $folder;
require_once($_SERVER['DOCUMENT_ROOT'].$folder."/order/includes/db.php");
..do stuff

}
Sign up to request clarification or add additional context in comments.

1 Comment

global $folder = "/test" through me an error but calling global $folder after the far was fine. I also found that require_once was an issue in that it had been called before and therefore was not giving me the variables in the include file. Using just require fixed that issue

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.