How to declare global array?

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
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

How to declare global array?

Post by ljCharlie »

How do I declare a global array in my page so that I can use this array anywhere on my page?

ljCharlie
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I think changing the variable which returns the array into a global variable should work.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You could also try out the $_SESSION['array_here'] or $GLOBALS['array_here'] approaches (sessions preferred).
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Thank you for your suggestion. Can you show me? I have tried global $myArray[]; but this doesn't work. I kept getting an error.

ljCharlie
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

$array = array('foo','bar');

Code: Select all

require 'page1.php';
function foo() {
 global $array;
 print_r($array);
}
// ...or...
function bar() {
 print_r($GLOBALSї'array']); // unsure this is correct tho...
}
( Untested, at work with no time, and crap keyboard. )
I'd personally use sessions however, and there is some good tutorials in the forum (check the sticky topics and try the search).
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It does help if you show us some of our code so that we have a better idea of what you are trying to accomplish.

Mac
Post Reply