[Resolved] Include/Require Problem
This problem has really bothered me for a long time, but I've just tried to get around it. Now it's getting to be annoying, so I'm hoping there's a solution.
I'm having a problem including/requiring a file within a function, and still having variables it created be defined through a few more function calls. I know that probably doesn't make sense, so I trimmed down my code to the bare-minimum for an example.
index.php is the main page, which calls a display.php (for displaying news in my program). display.php calls config.php, which simply loads configuration variables into an array.
Code:
index.php
<?php
// There would normally be some type of if structure
// here, that's why a function is needed:
main();
function main() {
require 'display.php';
execute();
}
?>
display.php
<?php
require 'config.php';
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'];
}
?>
config.php
<?php
// this usually loads values form a database
$CONFIG['name'] = 'value';
?>
Now, the execute() function of display.php is supposed to output a variable value, but no dice.
My question to you is, what can I do to make this work, besides fudging around in the index.php file (since I want my users to be able to call it however they need to)?