Click to See Complete Forum and Search --> : If not globals, what?


subnet_rx
11-23-2004, 02:24 PM
What's the new way to create a configuration file that every function and file has access to? Stacking it in $_SESSION?

Jona
11-23-2004, 02:30 PM
Originally posted by subnet_rx
What's the new way to create a configuration file that every function and file has access to? Stacking it in $_SESSION?


// include configuration file
// assumed in the /inc/ directory
// change accordingly.
// The DOCUMENT_ROOT is just the path to
// your location on the server.
include($_SERVER["DOCUMENT_ROOT"]."/inc/config.cfg.php");

subnet_rx
11-23-2004, 02:52 PM
are functions called within that page going to have access to the variables, or do you have to include that on every PHP page?

NogDog
11-23-2004, 03:00 PM
Originally posted by subnet_rx
are functions called within that page going to have access to the variables, or do you have to include that on every PHP page?
Whenever this sort of thing becomes an issue, it's always a good idea to take a step back and look at your code design. Global variables in any progamming language invariably become a maintenance nightmare. If a function needs to read a variable, make it an input parameter to that function instead. If a function needs to change a value, have it return a value instead and use it to change the value within your script.

Anyway, the official info on variable scoping is at http://us2.php.net/manual/en/language.variables.scope.php .

Jona
11-23-2004, 03:30 PM
Originally posted by subnet_rx
are functions called within that page going to have access to the variables, or do you have to include that on every PHP page?

Include the configuration file on every page you want access to configured variables; this way you can avoid using global variables. Moreover, you can maintain variables across multiple pages by file inclusion rather than having to use sessions (which I wouldn't recommend doing anyway). You can also use includes this way for a global set of functions accessible to every page that you want.