View Full Version : Session Array

12-21-2006, 03:20 PM
General Qstn;

Is it best practice when dealing wit lots of session variables. to.

Insert them in array as such, because i cant see what advantage it would b.

$_SESSION['serv'] = array();
$_SESSION['date'] = $_POST['date'];
$_SESSION['service'] = $_POST['service'];

12-21-2006, 03:34 PM
What you've shown there is exactly what I do. Whether it's "best practice" or not is open to opinion, I guess.

12-21-2006, 03:43 PM
Well, in your example you haven't actually done anything different, you declared $_SESSION['serv'] as an array, but you never use it after. Maybe you meant:

$_SESSION['serv'] = array();
$_SESSION['serv']['date'] = $_POST['date'];
$_SESSION['serv']['service'] = $_POST['service'];

if so, I can see advantages of this, quick examples that pop into my head are when dealing with shopping carts.

12-21-2006, 04:10 PM
If you meant as per Brandoe85's example then the advantage is that your are giving your variables a `namespace` of sorts , its really easy to end up with lots of session variables with names that may confuse ..


Is that the number of items in a cart or the number of login attempts or ..?
OK you can use $_SESSION['cart_num'] etc but personally I prefer the array access.

Variable naming conventions can save you hours of headaches for little effort, & I wish I had realised that the first time I was told so !