0

I try to find the solution since many hours, but I can't solve it (I'm not a programmer ;)).

On a function, I have set a dynamic array which I want to use in another function. To do this, I thought to use $GLOBALS[] array I have no problem to call the variable out of the function one, but when I try to use it in function 2, it doesn't works.

Here is my code :

function one($name,$a,$b,$c)
{
// $GLOBALS[${$name}] = array($a,$b,$c);
global $$name;
$$name = array($a,$b,$c);
}



function two($name)
{
$name="D1";
echo ${$name}[1];
}

one("D1",10,20,30);
one("D2",100,200,300);
two("D1");      // doesn't works


$name="D1";
echo ${$name}[1];   // works, gives 20
$name="D2";
echo ${$name}[1];   // works, gives 200

It doesn't works and I do not understand why. Thanks for your help.

Etienne

Etienne
  • 73
  • 8
  • 2
    http://php.net/manual/en/language.variables.scope.php – Chin Leung Oct 10 '17 at 13:25
  • 2
    Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – GrumpyCrouton Oct 10 '17 at 13:26
  • using `global $name;` could be easier – PaulH Oct 10 '17 at 13:27
  • Dear Paul, I tried your way, but I still have the same problem with the function two. 'function one($name,$a,$b,$c) { global $$name; $$name = array($a,$b,$c); } function two($name) { echo ${$name}[1]; } one("D1",10,20,30); one("D2",100,200,300); two("D1"); // doesn't works $name="D1"; echo ${$name}[1]; // works, gives 20 $name="D2"; echo ${$name}[1]; // works, gives 200' – Etienne Oct 10 '17 at 14:56

2 Answers2

2

how about doing something like this:

function one() {
  $dynamicArray = generateDynamicArray();
  return $dynamicArray;
}

function two() {
  $one = one(); // if it's in a class use: $this->one();
  foreach($one in $key=>$value) {
       // your code for each item in the array we got form function one() 
  }
}

instead of defining it globally.

popeye
  • 481
  • 1
  • 4
  • 16
  • Dear Chandlerbing, Thank you for your answer, but I'm not enough skilled to understand it. I clarified my question, hope it will be more clear for you. thanks – Etienne Oct 10 '17 at 14:22
1
<?php

function one($name,$a,$b,$c)
{
    global $$name;
    $$name = array($a,$b,$c);
}

function two($name)
{
    global $$name;
    echo ${$name}[0];
}

one("D1",10,20,30);
two("D1"); 

The scope of your function is different to the global scope.

Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Thank you very much Progrock ! – Etienne Oct 10 '17 at 15:13
  • I did not know that, I thought that the global scope was on all the document including all functions ! I would like to be sure to understand. To use the 'global' variable in another function, you need to declare it in the function using 'global'. I have just read again the french php user guide, I it written, but it was not so clear to me ;) So thank you very much ! – Etienne Oct 10 '17 at 15:25