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
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

session variables

Post by paqman »

This is probably a really dumb question but I can't find the answer anywhere else. When I create a session variable ($_SESSION["name"]) is it possible to use a variable for its name ($_SESSION[$name])? The session variable is called frequently and having it predefined at the top of the file would make changes quite easy. Thanks!
User avatar
Peter Anselmo
Forum Commoner
Posts: 58
Joined: Wed Feb 27, 2008 7:22 pm

Re: session variables

Post by Peter Anselmo »

Using a variable as an index for the session array is perfectly valid.

Code: Select all

 
$_SESSION['bar'] = "Hello World";
$foo = 'bar'
echo $_SESSION[$foo] //prints "Hello World"
Post Reply