Multiple session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Multiple session variables

Post by hame22 »

Hi I need to create a number of session variables.

I can make them all like this

Code: Select all

$_SESSION['product_category'] = "var 1";
$_SESSION['page_count'] = "var 2";
$_SESSION['duration'] = "var3";
and so on.....

however is there an easier way to store these variables rather than write out lines of code, im interested to know?

thanks in advance
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:?

How do you mean? It depends where the variables are comming from. If they come from a single array then I ges you can yes.

You could cut down the number of characters you type by using references or functions too:

Function:

Code: Select all

function setVar($name, $value)
{
    $_SESSION[$name] = $value;
}

setVar('foo', 'bar');
echo $_SESSION['foo']; //bar
References:

Code: Select all

$x = &$_SESSION;

$x['foo'] = 'bar';

echo $_SESSION['foo']; //bar
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

setting session variables is just like setting any variable.. thus this is possibly as vague a question as you can get for php.
Post Reply