Naming a session variable

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
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Naming a session variable

Post by Skittlewidth »

I'm trying to write a script for an online multiple choice test/exam, and thanks to my boss I have 4 days to do it.
I've been trying to swot up on sessions and I just want to know if the following is possible:
Can you use a variable as a variable name for a session variable? (Confused?)

Every time the user selects an answer and submits it, I want to write the value of that answer (A,B,C or D) to a new session variable. The name of this variable would be $_SESSION['answer_number of the question'].

The way the script would know what question the user had answered would be through another variable from the questions database. Can I just write $_SESSION['answer_$question_no'] or something similar, or is that just not possible?

I'm trying to avoid hardcoding the $_SESSION variable name into the script so that I don't have to have a new page per question (ie 40+ pages).

If the above made no sense at all, let me know and I'll try and explain a different way. In the mean time, I'll go test a few things out!
Thanks!
8)
Skittlewidth
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can definitely have dynamic array element names, e.g.:

Code: Select all

$array['prefix_'.$number] = 'something';
Mac
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Thanks for clarifying that. I've got that bit working now. Hopefully I can go on to panicking about something else now!
:wink:
Post Reply