tags:

views:

15

answers:

2

Is there any way by which I can add keys to superglobals in php without defining the corresponding values to those key?

For example:

$_SESSION['key']='set';//key` automatically gets defined.

But I want to do something like this

add_key($_SESSION,'key')//key is added to $_SESSION array.

Is it possible?

+4  A: 

It's possible, you just have to pass the first argument by reference. I'm not sure why you want it though:

add_key(array &$var, $key) {
    if (!array_key_exists($key, $var))
        $var[$key] = NULL;
}
Artefacto
Thanks for your reply.
gautam kumar
+2  A: 

Can't you just set it to an empty string? or empty? or NULL?

What is it you want to happen when you access $_SESSION['key'] after calling add_key($_SESSION, 'key')?

Carson Myers