Variable scope issue

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a function foo () in a file called funcfoo.php called from my main
program file main.php, which has an include for that file.

Foo () contains a line:

echo $bar;

Now I have a (global) variable $bar which is set in main:

GLOBAL  $bar;
$bar = 1;

When I call foo () from main it doesn't echo the value 1. (How) can I make
$bar visible to foo () without explicitly passing it as a parameter ? Is the
only way putting the entire foo function body in the same file as main ?


Re: Variable scope issue

Quoted text here. Click to load it

Try using GLOBAL  $bar; in function foo() before the echo. This is all
explained in

Tony Marston

Re: Variable scope issue

Tony Marston wrote:
Quoted text here. Click to load it

Thanks, that fixed it!

Re: Variable scope issue

Pjotr Wedersteers wrote:
Quoted text here. Click to load it

It has no effect to use the "global" modifier in the global scope.

Variable scope and the use of global is very well explained here:


Site Timeline