RE: [PHP] session variables and scope?
| From: | Lawrence dot Sheed at dfait-maeci dot gc dot ca | Date: | Tue, 03 Oct 2000 03:46:55 +0000 |
| Subject: | RE: [PHP] session variables and scope? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18362@lists.php.net to get a copy of this message | ||
If its inside a function which it looks like it is, you'll have to
explicitly declare it as global
eg
$foo = "bar";
function blat() {
echo $foo;
}
Calling blat will output nothing.
However
$foo = "bar";
function blat() {
global $foo;
echo $foo;
}
Will work as anticipated.
Have a look at php.net in the annotated php manual, rtfm is very useful :)
Cheers,
Loz.
-----Original Message-----
From: Scott Brown [mailto:scott-brown@home.com]
Sent: October 3, 2000 11:21 AM
To: php-general@lists.php.net
Subject: [PHP] session variables and scope?
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net