Variable scope and function inside function

Please consider the following scenario


$a = 'hi';
one();

function one() {
global $a;

$b = 'hi';
echo $a;
echo '-';
echo two();
}

function two() {
global $b;
echo $b;
}

This will produce

hi

instead of

hi-hi

What is the workaround of such a limitation if i can call it?

don’t use globals, send parameters instead:


$a = 'hi';
one($a);

function one($a) {
  $b = 'hi';
  echo $a;
  echo '-';
  two($b);
}

function two($b) {
  echo $b;
}

Thank you; I am currently doing your proposal to solve such an issues, but I’m trying to find a way without passing arguments…

If I may… why?

At some point it might be needed, and also simply to learn whether that possibility exists in PHP or not…

global calls for variables in the global scope.

$b is not in the global scope. Try globalizing $b inside your one function.

If at any point you ~need~ globals you’re doing something horribly wrong. Seriously.

qft.