> -----Original Message-----
> From: Joe Nelson [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, March 28, 2001 6:17 PM
> To: php list
> Subject: [PHP] Variable scope problem
>
>
> I have two functions like the ones below.  If the first one creates a
> variable as global, shouldn't it be accesible to the second function?
>
> function setGlobal() {
>       global $test;
>
>       $test = "123";
> }
>
> function getGlobal() {
>       global $test;
>
>       echo $test;
> }

Global doesn't CREATE global variables, it allows variables to be used as
globals.  For example, in the above setup, you would do this instead:

function setGlobal() {
        global $test;

        $test = "123";

        return $test;
 }

$test = setGlobal();

function getGlobal() {
        global $test;

        echo $test;
 }

And now, getGlobal will echo $test as:

123

Of course, if you are looking to create global variables in a single
function, then you simply would do something like this:

function setGlobal() {
        global $test;

        $test[0] = "123";
        $test[1] = "456";
        $test[2] = "789";

        return $test;
 }

and that would return the array $test, and therefore:

$test = setGlobal();

$test would be an array, and then you could set global in any function $test
and be able to use multiple values (as per the array).  This is easier than
globaling 20 or so values you may need.

Jason Lotito
www.NewbieNetwork.net
Where those who can, teach;
and those who can, learn.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to