Sessions and variable scope strangeness
| From: | Jacob Hanson / Electric Lucidity | Date: | Wed, 12 Jul 2000 19:47:02 +0000 |
| Subject: | Sessions and variable scope strangeness | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-6241@lists.php.net to get a copy of this message | ||
Why is it, that in order to correctly register a variable to a session
in a function, you have to declare the variable global.
Let me illustrate:
<?php
function dosession(){
session_start();
$nio = "pizza";
session_register("nio");
}
dosession();
print "Value of 'nio' is: $nio";
?>
Logically, (I think) this code should work, but it doesn't.
The only way to get $nio to register correctly is to
declare it as global and then give it a value:
<?php
function dosession(){
session_start();
global ($nio);
$nio = "pizza";
session_register("nio");
}
dosession();
print "Value of 'nio' is: $nio";
?>
This works. I just don't understant why $nio needs to be
declared global in order to register with the session correctly.
Does anyone know?
...
_/_/_/ _/ Jacob Hanson
_/_/ _/ mailto:hello@electriclucidity.com
_/_/_/ _/_/_/ http://www.electriclucidity.com