p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: is there a way to include variables?


Message #1 by spam@k... on Sat, 21 Sep 2002 20:53:04 -0500
The below doesn't work. Nothing is echoed to the screen. There must be some way to include a bunch of
variables in a function. What is the method most people use? I could hardcode the address of the include in every single function
that I write, but that seems like a very bad idea. 

(the address is correct and $host is a valid variable)


function includeConfig() {
	include("mcIncludes/mcConfig.php");
}

function showConfig() {
	includeConfig();
	echo $host;
}

showConfig();
Message #2 by spam@k... on Sun, 22 Sep 2002 03:39:32
Okay, the only way to do it is to build a big array in your config file 
like this:

config['host'] = "sitedb.hostcompany.com";
config['dbPassword'] = "superSecret";
etc...

And then you can include the config file and return this giant array, and 
then, as soon as you've got it in your function, hit it with extract(). 

By the way, one problem that I struggeld with for a while was how to get 
$PHP_SELF into a variable like $self in the config file, and have it hold 
its value into a function. Nik explained why it was so hard: if you 
include a file, the variables act as if they are being declared on that 
line, inside that function. So a global variable is empty, since it 
hasn't yet been declared local to that function. 

The solution I finally came up with was this:

global $PHP_SELF;
$self = $PHP_SELF;

You can do this in the config file. 

As to why I'm still using $PHP_SELF, my own website is still running on 
PHP 4.0x (I forget the last number). 




  Return to Index