1

I would like to set the name of a $_SESSION variable to be the value of another variable. e.g.

$var = "123";
$_SESSION['$var'] = "yes";

echo $_SESSION['123'];  // Would like to echo "yes"
echo $_SESSION['$var'];  // Would also like to echo "yes"

I performed a vardump($_SESSION) and the session variable is literally set to "$_SESSION[$var]". How can I achieve the result I require?

Many Thanks.

1
  • It works [almost] exactly like an "array" .. read up on it. Commented Feb 22, 2013 at 3:12

1 Answer 1

8

Ditch the single quotes. They make PHP parse the $ as a literal dollar sign instead of the beginning of a variable name.

$_SESSION['$var'] = "yes";

should be:

$_SESSION[$var] = "yes";
Sign up to request clarification or add additional context in comments.

2 Comments

Or could be in double quotes of he really wants to keep the quotes or have to do concatenation (eg prefix to name)
Thanks so much. A silly error tbh. Maybe it's time to stop coding and get some sleep. lol. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.