Page 1 of 1

how to make an array global

Posted: Sat Mar 14, 2009 4:35 pm
by katkat
Is there a way to make an array global?
I load it in one function and need to use it in several other functions.

Thanks!

Re: how to make an array global

Posted: Sat Mar 14, 2009 4:37 pm
by Benjamin
Each function will need to define it as global. This is very poor programming practice. I'd encourage you to find another solution.

Code: Select all

 
function foo() {
    global $myArray;
 
}
 

Re: how to make an array global

Posted: Sat Mar 14, 2009 4:45 pm
by katkat
Well, for some reason it is empty.

I know it has data in it at the end of a function because I dump it.
When I execute the next function, I try to dump it immediately but it display "null".

Any other suggestions?

Re: how to make an array global

Posted: Sat Mar 14, 2009 4:46 pm
by Benjamin
Nope. That's how you do it.

Re: how to make an array global

Posted: Sat Mar 14, 2009 4:52 pm
by katkat
Do I need to include the array names in the function's variables list?
Do I need to include

global $myarray;

in each of the functions?

Re: how to make an array global

Posted: Sat Mar 14, 2009 5:30 pm
by php_east
you also have to declare it global in the beginning, *before* declaring it global again in each function, and yes, in each and every function you wish to have access to it.