RE: [PHP] Session variable scope - surprise!
| From: | Richard Black | Date: | Mon, 04 Mar 2002 15:09:39 +0000 |
| Subject: | RE: [PHP] Session variable scope - surprise! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-87100@lists.php.net to get a copy of this message | ||
I think session_register registers the variable and its value as a global.
In this case, $test's global value is nothing. nada. Because it doesn't
exist globally.
In the reg() function, what is being output is a LOCAL variable called
$test. Not a global value.
If you put the line
global $test;
into the reg() function so that it reads:
function reg() {
global $test;
$test = 13;
session_register("test");
echo "in reg(), test=<$test><br>";
}
Then it should work as expected. Sure it *does* say in the docs somewhere
that register_globals() is only applicable to global variables.
HTH,
Richy
==========================================
Richard Black
Systems Programmer, DataVisibility Ltd - http://www.datavisibility.com
Tel: 0141 435 3504
Email: rblack@datavisibility.co.uk
-----Original Message-----
From: Richard Fox [mailto:rfox@sbsii.com]
Sent: 04 March 2002 14:57
To: php-general@lists.php.net
Subject: [PHP] Session variable scope - surprise!
This is a behavior I have not found documented, in fact the books I have
read indicated that this would not be the case...
given the script (with session.save_handler = files):
<?php
function reg() {
$test = 13;
session_register("test");
echo "in reg(), test=<$test><br>";
}
session_start();
reg();
echo "test=<$test><br>";
?>
The output is:
in reg(), test=<13>
test=<>
I was quite surprised, but at least now I know why my code isn't working. I
assume that $test is going out of scope after the function reg() exits. BUT,
isn't registering a variable as a session variable supposed to give it
"superglobal" status? I thought so. Can someone give me the 'official'
explanation of this behavior?
Many thanks,
Richard
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php