-1

Why does echoing the variable from test1() in test2() in the following code not work? I thought if I execute a function in another one it is jsut as if the code was placed in there. And how can I do it to make it work?

function test1() {
    $var = "Hallo";
}

function test2() {
    var();
    global $var;
    echo $var;
}

test2() ;
ink
  • 223
  • 5
  • 13
  • 1
    Suggest that you read about [variable scope](http://php.net/manual/en/language.variables.scope.php) – Mark Baker Jul 07 '15 at 21:04
  • I think I do generally understand the scope but I am just confused about how one function in another works with passing variables. – ink Jul 07 '15 at 21:26
  • You don't have "one function inside another", you have one function __being called__ from inside another.... and every function (no matter how it is called) has its own unique scope – Mark Baker Jul 07 '15 at 21:30
  • Yes my bad for expressing me badly. But what also after reading about scopes is how I get multiple variables form one functions into another. Could you explain me how to do this? – ink Jul 07 '15 at 21:33
  • You pass arguments to a function, and return values from a function – Mark Baker Jul 07 '15 at 21:49

1 Answers1

1

The $var variable isn't in the scope of the test2() function.

See: this post for details.

Rob
  • 755
  • 1
  • 4
  • 14
  • Edited my question. I tried changing it's scope to global, why is it it then still not working? The thing is that the code really has to be in two functions and that is why I can not pass the variable as an argument. – ink Jul 07 '15 at 21:19