StormTheGates Posted May 5, 2008 Share Posted May 5, 2008 Hello there. I need to do something like this: $question1$answer++; Basically an example of the output would be like $question12++; How do I do this? I remember having to do something with globals a while back. Quote Link to comment https://forums.phpfreaks.com/topic/104198-variables-inside-variables/ Share on other sites More sharing options...
MadTechie Posted May 5, 2008 Share Posted May 5, 2008 Huh! you lost me, what are you trying to do ? Quote Link to comment https://forums.phpfreaks.com/topic/104198-variables-inside-variables/#findComment-533414 Share on other sites More sharing options...
obsidian Posted May 5, 2008 Share Posted May 5, 2008 I don't believe you can do it in the middle of the variable name like that, but you can do this: <?php $ans = 'question12'; ${$ans}++ ?> Quote Link to comment https://forums.phpfreaks.com/topic/104198-variables-inside-variables/#findComment-533416 Share on other sites More sharing options...
StormTheGates Posted May 5, 2008 Author Share Posted May 5, 2008 $q1{$questions[0]}++; Would that work? Whats drawn from the database is question answers 1-5. So I have variables like $q11 $q12 $q13 $q14 $q15 and I want to just be able to easily increment one without the use of if statements. So I was hoping to do $q1$answer to incrent say $q12 if the answer is 2. Quote Link to comment https://forums.phpfreaks.com/topic/104198-variables-inside-variables/#findComment-533426 Share on other sites More sharing options...
MadTechie Posted May 5, 2008 Share Posted May 5, 2008 maybe something like this <?php $q[1] = "What says meow!"; $a[1][1] = array("Cat",true); $a[1][2] = array("Dog",false); $a[1][3] = array("Snake",false); foreach($q as $k => $qs) { echo $qs; shuffle($a[$k]); foreach($a[$k] as $L) { echo $L[0]; if($L[1]) echo "Correct"; echo "<br>"; } } ?> EDIT: this is untested Quote Link to comment https://forums.phpfreaks.com/topic/104198-variables-inside-variables/#findComment-533436 Share on other sites More sharing options...
PFMaBiSmAd Posted May 5, 2008 Share Posted May 5, 2008 Any time you have a set (two or more) of related data (a series of questions and answers), use an array to hold and manipulate the values. That is what arrays are for. Don't use sequentially named variables. Quote Link to comment https://forums.phpfreaks.com/topic/104198-variables-inside-variables/#findComment-533449 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.