This is a discussion on Variable scope issue within the PHP Language forums, part of the PHP Programming Forums category; I have a function foo () in a file called funcfoo.php called from my main program file main.php, which ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
"Pjotr Wedersteers" <pjotr@wedersteers.com> wrote in message news:417be35f$0$36860$e4fe514c@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 |
|
|||
|
Tony Marston wrote:
> "Pjotr Wedersteers" <pjotr@wedersteers.com> wrote in message > news:417be35f$0$36860$e4fe514c@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! |