how to make an array global

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

how to make an array global

Post 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!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: how to make an array global

Post 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;
 
}
 
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: how to make an array global

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: how to make an array global

Post by Benjamin »

Nope. That's how you do it.
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: how to make an array global

Post 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?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: how to make an array global

Post 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.
Post Reply