Variable scope issue

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-24-2004
Pjotr Wedersteers
 
Posts: n/a
Default Variable scope issue

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



Reply With Quote
  #2 (permalink)  
Old 10-24-2004
Bent Stigsen
 
Posts: n/a
Default Re: Variable scope issue

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
Reply With Quote
  #3 (permalink)  
Old 10-24-2004
Tony Marston
 
Posts: n/a
Default Re: Variable scope issue


"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



Reply With Quote
  #4 (permalink)  
Old 10-25-2004
Pjotr Wedersteers
 
Posts: n/a
Default Re: Variable scope issue

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!


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:19 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0