Reputation: 4508
Let's say we have
$_SESSION["number"]=1;
and
$_SESSION["color1"]='red';
What I want is to write something like
$_SESSION["color".$_SESSION["number"]]='red';
But unfortunately it cannot be done the way I'm trying. Any other way to do such trick ?
Upvotes: 1
Views: 68
Reputation: 93
You can use like this:
$number = $_SESSION["number"];
$_SESSION["color".$number]='red';
Upvotes: 0
Reputation: 5849
This works:
$key = 'color'.$_SESSION['number'];
$_SESSION[$key] = 'red';
Edit After checking, turns out your first guess also works as well.
Upvotes: 0
Reputation: 1260
Try this,
$_SESSION["number"]=3;
$_SESSION["color1"]='red';
$_SESSION["color{$_SESSION["number"]}"]='red';
Upvotes: 1