1

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.

2
  • What's wrong with declaring f1() as function f1(&$array) { ... }? Commented Oct 2, 2012 at 15:38
  • @Jack, nothing, I should may be do it like that. Commented Oct 2, 2012 at 15:42

3 Answers 3

5

You have to call global inside each of the functions so PHP knows to look outside of the local scope.

function f1()
{
   global $array;
   $array[0] = $array[0]." and test1";
}

function f2()
{
   global $array;
   $array[0] = $array[0]." and test2";
}

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

function f3(&$array)
{
    $array[0] = $array[0]." and test3";
}

$array = array();
$array[0] = "test";

f3($array);
var_dump($array);

You can see an example at: http://codepad.org/27R5ZuKM

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you @Pez Cuckow, I should maybe avoid globals and use local array.
Hm, but stil, when I am trying to use global like that, I am not affecting the global variable in the function, only the local one.
Both these options will modify the global variable, please try again!
I still think that since arrays are typically passed by copy instead of &reference, you should possibly do $array = f3($array) when you call the function, and return the array at the end of the function.. even if you are passing it by reference. Little things like this aid in readability.. vs having something that looks like an idempotent function call modify a passed variable.
@Anther are you saying pass by reference but leave $array = f3($array) just to make it clearer? If so, this makes sense
|
2

How about passing a parameter to a function and returning results?

avoid one big global spaghetti:

function f1( $array )
{
   $array[0] = $array[0]." and test1";
   return $array;
}

function f2( $array )
{
   $array[0] = $array[0]." and test2";
   return $array;
}

2 Comments

The problem for me is that I have to return a boolean in the function f1 and f2 in my code.
@Milos That function's sounding a little smelly to me. It modifies an array and then does some sort of comparison that it has to return a boolean? It seems like the comparison or validation should be a separate function.. since functions are typically supposed to have one job only.
1

You need to declare the array as global in the local scope, i.e. in the function.

$array[0] = test;

if (something) function f1() else function f2();

function f1()
{
   global $array;
   $array[0] = $array[0]." and test1";
}

function f2()
{
   global $array;
   $array[0] = $array[0]." and test2";
}

3 Comments

Appreciate it's hard to do differently but this looks very c&p'd from my answer (check edit log it was after I posted mine)
yes it was... I was alrready editing when I got the '1 new Answer' and I didn't check!!! My Bad, should have.
As you'll see it was copied pasted from the question and then edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.