Re: Question on functions

From: Date: Wed, 10 Nov 2004 14:32:53 +0000
Subject: Re: Question on functions
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-201671@lists.php.net to get a copy of this message
Or you can use superglobal variable $GLOBALS that is array containing all GLOBAL variables. This variable is superglobal thus does not need to be declared global using 'global $GLOBALS;' statement. Works anywhere. function database() { ... $GLOBALS['array'][0], $GLOBALS['array'][1] ... } elixon Tip: I recommend to not use GLOBALS for this type of data. Better use function parameters -> function database($host, $user, $pwd, $db) {...} It will save you time when your application grows and starts clashing with other global variables or third party pieces of code ;-) Jason Gerfen wrote:
John Holmes wrote:
Jason wrote:
My question is in regard to passing global variables to a function. Here is my code, any idea why it is not working? I suppose my understanding of a global variable being able to be used within a function is off? global $array = array( "0" => "hostname", "1" => "username", "2" => "password" ); function database() {
You need to declare it global within the function... function database() { global $array; ...
thanks, it figures it is something easy like that... =)


« previous php.general (#201671) next »