I would like to set a session variable with something akin to:

$key = '_SESSION[element]';
$$key = 'value';

This does indeed set $_SESSION['element'] equal to value, but it also seems to clear the rest of my $_SESSION variable, resulting in the $_SESSION array only containing the new key/value pair.

How can I write into the session using variable variables without nuking it?

Edit: if this can't be done, so be it, we'll probably have to restructure and do things the "right" way. I just wanted to know if there was an easy fix

share|improve this question

2  
Please elaborate why you can't just use an ordinary $_SESSION["$key"] array access. – mario Oct 14 '11 at 6:25
why you want to use variable variables? – Gianps Oct 14 '11 at 6:36
@mario: this piece of code handles a lot of (non-session) variable assignments, and I can't edit it (without approval, etc) - my module only has control over what keys and values it sends in. If this can't be done we'll restructure and do it "right" but if there was an easy fix to get this done from my module alone it would be lovely – Mala Oct 14 '11 at 7:02
@Mala have you tried the solution I posted earlier in my edited answer? It should work like you expected – Kemal Fadillah Oct 14 '11 at 7:08
2  
To answer your question: No, variable variables can only reference another variable base name. They are not variable expressions by themselves. It's an edge case that your assignment succeeded oddly. – mario Oct 14 '11 at 7:14
show 2 more comments
feedback

3 Answers

From PHP Documentation:

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.

How you ended up with a situation like this, is really questionable. You're probably doing something wrong.

EDIT

This little trick should give you what you want:

$key = '_SESSION[element]';
$key = str_replace(array('_SESSION[', ']'), '', $key);
$_SESSION[$key] = 'value';
var_dump($_SESSION);

This will basically produce the same results as xdazz's answer

share|improve this answer
Regarding the quote from the docs, yeah I saw that, but I'm not in a function or method in this part of the code. I know doing it this way is "wrong" and it'd be better to make this not necessary, but if a 5-minute fix will save a 50-hour restructure in a block of throwaway code, i'm all for it. – Mala Oct 14 '11 at 6:32
With respect to the code you provided, unless I'm mistaken, that's reading the variables, not writing... – Mala Oct 14 '11 at 6:34
feedback

Isn't this way better?

$key = 'element';
$_SESSION[$key] = 'value';
share|improve this answer
feedback

@Mala, I think eval will help you. Check the code below. It may help you for what you want.

session_start();
    $_SESSION['user1'] = "User 1";
    $_SESSION['user2'] = "User 2";

    $key = "_SESSION['user3']";
    eval("\$$key = 'User 3';");

    foreach ($_SESSION as $key=>$value){
        echo $key." => ".$value."<br/>";
        unset($_SESSION[$key]);
    }
    session_destroy();

If you still have any trouble, Let me know. Thank you

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.