golden14 Posted March 8, 2008 Share Posted March 8, 2008 I have many variables (about 50) coming into PHP from Flash, and I wanted to optimize my PHP code using a loop, but I can't seem to get the correct syntax. Here is the working code I have so far (using just 3 as an example): <?php $tName1=$_POST['tName1']; $tName2=$_POST['tName2']; $tName3=$_POST['tName3']; echo "tName1=".$tName1; echo "tName2=".$tName2; echo "tName3=".$tName3; ?> And I would like to do that in a loop. Here's my incorrect attempt at that: <?php for ($i=1; $i<4; $i++) { $tName[$i]=$_POST['tName[$i]']; echo "tName$i"=.$tName$i; } ?> Any suggestions/help would be much appreciated, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/ Share on other sites More sharing options...
BlueSkyIS Posted March 8, 2008 Share Posted March 8, 2008 <?php for ($i=1; $i<4; $i++) { ${'tName'.$i}=$_POST['tName'.$i]; echo "tName$i = ".${'tName'.$i}; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/#findComment-487180 Share on other sites More sharing options...
Naez Posted March 8, 2008 Share Posted March 8, 2008 Variable variables, introduced in PHP5 I do believe <?php for ($i=1; $i<4; $i++) { $tName = "tName" . $i; $tName = $_GET[$tName]; echo "tName" . $i . "=" . $tName . "<br />"; } ?> I used GET for when I tested (you can change that) I accessed it from http://localhost/PHP/test.php?tName1=testing1&tName2=lalalalal&tName3=test3 my output was tName1=testing1 tName2=lalalalal tName3=test3 Hope this was useful!! Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/#findComment-487184 Share on other sites More sharing options...
BlueSkyIS Posted March 8, 2008 Share Posted March 8, 2008 ^ that's okay if you don't need those variables later. but if you need them later use my version. Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/#findComment-487185 Share on other sites More sharing options...
golden14 Posted March 8, 2008 Author Share Posted March 8, 2008 Thank you both for the help. The first bit of code didn't seem to work, so I tried the second way (changing GET to POST), and that worked well. You mention that doing it the second way won't allow me to access the variables later on, but since I POST instead of GET. Was that different in not allowing me to access them again later? Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/#findComment-487192 Share on other sites More sharing options...
BlueSkyIS Posted March 8, 2008 Share Posted March 8, 2008 what's different in the second one is that the variable $tName is used over and over. after the loop, it is set to the last value but you won't have any before the last one. my version is working fine here: <?php // set these for testing $_POST['tName1'] = "var1"; $_POST['tName2'] = "var2"; $_POST['tName3'] = "var3"; for ($i=1; $i<4; $i++) { ${'tName'.$i}=$_POST['tName'.$i]; echo "tName$i = ".${'tName'.$i}."<BR>"; } ?> [code] output: tName1 = var1 tName2 = var2 tName3 = var3 [/code] Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/#findComment-487193 Share on other sites More sharing options...
Naez Posted March 8, 2008 Share Posted March 8, 2008 in my example, you can't access $tname1,$tname3, $tname3, etc from outside of the loop (because theoretically it does not exist). Also BlueSkyIS's script runs fine on my server. I haven't played around a whole lot in variable variables but his example is great. Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/#findComment-487194 Share on other sites More sharing options...
golden14 Posted March 8, 2008 Author Share Posted March 8, 2008 Thanks for that explanation, that makes sense (too much sense now that I think about it). I had to make a few small changes, because due to the nature of how it comes back into the various text fields in flash, I had to use an "&" to split it up instead of the "<BR>". So here's the code now, and this does work correctly. Thanks for the help and taking the time to explain things, I really appreciate it. <?php for ($i=1; $i<4; $i++) { ${'tName'.$i}=$_POST['tName'.$i]; if($i>1){ echo "&tName".$i. "=".${'tName'.$i}; } else echo "tName".$i. "=".${'tName'.$i}; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/95104-correct-syntax-for-loopvariable-names/#findComment-487205 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.