Re: session variables and scope?
| From: | David Robley | Date: | Tue, 03 Oct 2000 03:40:38 +0000 |
| Subject: | Re: session variables and scope? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18361@lists.php.net to get a copy of this message | ||
On Tue, 03 Oct 2000, Scott Brown wrote:
> Preface: I'm new to PHP... I've only been coding in it for a week or two -
> my "day job" has me work in ASP ...
>
> I'm having a real weird problem here... Each of my pages (by virtue of a
> standardized header) starts with a session initialization of:
>
> <?
> session_start();
> if (!session_is_registered('dollarmode')) {
> session_register('dollarmode');
> $dollarmode="US";
> }
> ?>
>
> And inside my page, I require(..) various standardized routines, such as the
> site menu, and a set of common MySQL database routines... one of which gets
> me the current exchange rate for the US <-> Canadian dollar. (or as current
> as I decide it needs to be by manual updates of the database ;-)
>
> inside the menu routine, I have this:
>
> <a class=menu hreg="/togglemode.php"><?
> echo "<!-- " . $dollarmode . " -->";
> if ($dollarmode=="CAN") {
> echo "Show US$ Prices";
> else
> echo "Show CDN$ Prices";
> ?></a>
>
> And all works well - the toggle mode routine successfully toggles the value
> of $dollarmode between "US" and "CAN" without error. Viewing the resulting
> HTML sourse always shows me a valid value of $dollarmode
>
> One of my other routines, inside my sub.php include file, is called
> GetMultiplier, which uses $dollarmode to determine if it needs to get the
> exchangerate, or just default it to 1, I cant seem to reference $dollarmode
> at all.
>
> Inside this function, a
>
> echo "<!-- " . $dollarmode . " -->"
>
> produces nothing for dollar mode - the resulting line is
>
> <!-- -->
>
> (ie, just the TWO spaces between the comment start and end tags...) If I
> copy the if statement above into the GetMultiplier routine, I always see it
> showing "Show CDN$ Prices"..... It's like $dollarmode doesnt exist inside
> the function at all.....
>
> And references to $dollarmode AFTER the call to GetMultiplier do show a
> valid value of $dollarmode... so I know it's not being reset somewhere.
>
> Is there a scope problem I've overlooked somewhere along the line? This is
> driving me absolutely bonkers, as ASP session variables are available
> everywhere...
>
> Thanks
> Scott.
In detail, the answer is at
http://www.php.net/manual/language.variables.scope.php
In short, you need to declare $dollarmode gloabal in your function,
otherwise it'll be seen as a local variable in the function. Make the first
line of your function
global $dollarmode
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA