David
David

Reputation: 4508

Using first sessions value in second sessions name

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

Answers (3)

jems
jems

Reputation: 93

You can use like this:

$number = $_SESSION["number"];

$_SESSION["color".$number]='red';

Upvotes: 0

D4V1D
D4V1D

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

Rakesh Singh
Rakesh Singh

Reputation: 1260

Try this,

$_SESSION["number"]=3;
$_SESSION["color1"]='red';

$_SESSION["color{$_SESSION["number"]}"]='red';

Upvotes: 1

Related Questions