I’m setting a $baseurl in a settings.php file.
In my index.php, I’ve got 3 require() in a row for the settings.php/header.php/masthead.php.
When I come to echo out the $baseurl it’s undefined. If I define the $baseurl within the header.php then it works within the header.php file.
How can I get the $baseurl to be defined within the settings.php file and usable within each require() ?
Order of inclusion
The file that sets
$baseurlmust berequire_once()first for the subsequent files to have access to the variable.Function Scope
If you are defining
$baseurlin a function or the files arerequire_once()from within a function then$baseurlwill be trapped in that functions scope. This would the case ifsettings.phplooked something like:Or requiring from within a function
This is documented in the Variable Scope portion of the PHP Manual.
One way to work around this is to add
$baseurlas an element in$GLOBALSarray instead of as a standalone variable:Note I have added the
['config']element to namespace your config away from anything else you may be tempted to place in$GLOBALS.variable
unset()Another possibility is that you might be calling
unset($baseurl)somewhere else in the code, which would be marking the variable as undefined.