WebDude0482
WebDude0482

Reputation: 760

PHP Includes/Require in a function with variable for path

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?

Upvotes: 0

Views: 39

Answers (1)

Vitor Rigoni
Vitor Rigoni

Reputation: 573

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

}

Upvotes: 1

Related Questions