Page 1 of 1

How to declare global array?

Posted: Thu Oct 14, 2004 1:29 pm
by ljCharlie
How do I declare a global array in my page so that I can use this array anywhere on my page?

ljCharlie

Posted: Thu Oct 14, 2004 1:48 pm
by Joe
I think changing the variable which returns the array into a global variable should work.

Posted: Thu Oct 14, 2004 2:00 pm
by JAM
You could also try out the $_SESSION['array_here'] or $GLOBALS['array_here'] approaches (sessions preferred).

Posted: Thu Oct 14, 2004 2:01 pm
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

Posted: Thu Oct 14, 2004 2:09 pm
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).

Posted: Fri Oct 15, 2004 3:01 am
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