By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

Variable scope issue

P: n/a
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 ?

TIA
Pjotr

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Pjotr Wedersteers wrote:
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;


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:
http://www.php.net/manual/en/languag...bles.scope.php

/Bent
Jul 17 '05 #2

P: n/a

"Pjotr Wedersteers" <pj***@wedersteers.com> wrote in message
news:41***********************@news.xs4all.nl...
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
?

TIA
Pjotr


Try using GLOBAL $bar; in function foo() before the echo. This is all
explained in http://www.php.net/manual/en/languag...bles.scope.php

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #3

P: n/a
Tony Marston wrote:
"Pjotr Wedersteers" <pj***@wedersteers.com> wrote in message
news:41***********************@news.xs4all.nl...
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 ?

TIA
Pjotr


Try using GLOBAL $bar; in function foo() before the echo. This is all
explained in http://www.php.net/manual/en/languag...bles.scope.php


Thanks, that fixed it!
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.