2

I want to create a session variable but the session variable name I want to be dynamic. So what I need is the proper syntax for a variable name that is a $_SESSION variable.

I have tried the code below which creates a variable variable name and stored a value to it and it works just fine.

$xvalue = $_SESSION['delivery_id'];
$delivery_string = 'this_delivery_id_' . $xvalue ;
$$delivery_string = $_SESSION['delivery_id'];
echo "Variable name:&nbsp;" . $delivery_string . "<br/>";
echo "Session Variable Value:&nbsp;" . $this_delivery_id_29 . "<br/>";

The above code echos 29 for line 5; the desired result.

So working upon what worked with a variable variable I then just tried to make the variable name a $_SESSION variable name instead.

$value = $_SESSION['delivery_id'];
$xsession = "$_SESSION[\"" . $value . "\"]";  // this gives compilation error so dead stop. I also tried without the escapes and also a dead stop. I also tried escaping the [ and ] and got rid of the compilation error so the code runs but it does not give the desired result.
$$xsession = $_SESSION['delivery_id'];
echo "Variable name:&nbsp;" . $xsession . "<br/>";
echo "Session Variable Value:&nbsp;" . $_SESSION["delivery_id_29"] . "<br/>";

So line 2 of the code is where the problem is.

6
  • Not entirely sure what you are trying to do, but you will probably need to escape the $ in "$_SESSION[\"" . $value . "\"]" - "\$_SESSION[\"" . $value . "\"]" Commented Jun 4, 2019 at 19:17
  • 1
    This is horrible, abandon this right now. Just use arrays as intended. Commented Jun 4, 2019 at 19:35
  • In Woocommerce I allow users to purchase multiple items in an order. But each item can belong to a different delivery date (products are delivered in person on a certain date). So I need to know which delivery each item belongs to. Now I am setting a session variable of the delivery for the item when they enter the shopping path. And then when the order is completed I create post_metadata for the order items but the session variable I set only has the value of the delivery for the last item put in the cart. Thus I need variable session variable names to be able to save the post_metadata. Commented Jun 4, 2019 at 19:42
  • One example of an alternative approach would be to simply store the item IDs in a nest of dates. For example, if I order one item to be delivered on one day, I may produce $_SESSION['order_setup'] = ['2019-06-04' => ['foo-123']], or if I had 3 items to arrive on two different days, I may have: $_SESSION['order_setup'] = ['2019-06-04' => ['foo-123'], '2019-06-05' => ['bar-987', 'baz-456']]. It's a much simpler approach without varvars, and somewhat easier to iterate over. Commented Jun 4, 2019 at 19:50
  • 1
    Thanks a bunch. I'll give this a try. Commented Jun 4, 2019 at 20:28

1 Answer 1

0

The issue comes from trying to interpolate the variable a little "too hard," if you escape $_SESSION[$value] you'll be left with the string "$_SESSION[$value]", which is not a valid name for a variable - you're attempting to access the variable as if it were defined like so: $$_SESSION[$value] = 'foo';. What you want to be doing is taking the value of that array element and using that in the variable variable, which needs to be done by referencing it.

Either of the following seem to give the result you are going for; a straight variable variable:

$value = 'foo';
$_SESSION['foo'] = 'bar';
$bar = 'baz';

echo ${$_SESSION[$value]}; //prints baz

One with another step, aiding in making it more clear:

$identifier = $_SESSION[$value];
echo $$identifier //prints baz

I don't understand what you would be storing in this manner, but you may also investigate alternatives to achieve a cleaner, more straight-forward approach. If you clarify your use behind this, maybe someone will be able to suggest an alternative methodology.

Sign up to request clarification or add additional context in comments.

Comments

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.