note 10274 deleted from language.variables.scope by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 21:46:29 +0000 |
| Subject: | note 10274 deleted from language.variables.scope by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22236@lists.php.net to get a copy of this message | ||
In a function that registers a var into the session, you must first in that function declare that
var global. Then, in the function that references this session var, make the $HTTP_SESSION_VARS
array global, then copy the needed var from it to a new var for easy reading.
Example:
<pre>
function foobar() {
global $cust;
$cust["greeting"] = "hello";
session_register("cust");
bar();
.....
function bar() {
global $HTTP_SESSION_VARS;
$cust = $HTTP_SESSION_VARS["cust"];
print $cust["greeting"]; // Prints "hello"!
</pre>
Repeated in more detail:
A session var has to be declared global to be visible in a function, just like any other global var
does. (This is especially true in the registering function, ie, in the case where it is a function
that registered the session variable, because if this function did not first declare this var
global, the "registered" variable's values will be inaccessible once the function is
exited (this is true at least for array vars)). In addition, in functions the session vars are
only accessible thru the $HTTP_SESSION_VARS variable (unless passed in via function parameters, of
course): using "global" with the session var itself won't help. But note that the
$HTTP_SESSION_VARS array is directly usable for reading values only; assignment does not work (at
least in the main script; I read in the errata that assignment to this array will work in functions
if the array is passed by reference). (This is all with track_vars and register_globals enabled,
the default).