I need an global array in php.
Here is the example:
global $array;
$array[0] = test;
if (something)
function f1()
else
function f2();
function f1()
{
$array[0] = $array[0]." and test1";
}
function f2()
{
$array[0] = $array[0]." and test2";
}
But the problem is that the array is not affected as global but as local.
Do you have an idea why ?
Thank you.
You have to call global inside each of the functions so PHP knows to look outside of the local scope.
Note in the ‘real world’ you should probably avoid using globals wherever possible as the globals can usually be solved with refactoring or redesign. Globals tend to lead towards a big ball of mud.
You should consider pass by reference
You can see an example at: http://codepad.org/27R5ZuKM