benphp Posted September 18, 2008 Share Posted September 18, 2008 I have a list like: <?php $lastQ0 = ""; $lastQ1 = ""; $lastQ2 = ""; $lastQ3 = ""; $lastQ4 = ""; $lastQ5 = ""; $lastQ6 = ""; $lastQ7 = ""; $lastQ8 = ""; $lastQ9 = ""; ?> But what I really want to do is something like: <?php $count = 10; for ($i=0;$i<=$count;$i++) { $lastQ.$i = ""; } ?> I know that this won't work - so how do you do this? Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/ Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Use these brackets: {} <?php $count = 10; for ($i=0;$i<=$count;$i++) { $lastQ{$i} = ""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-644970 Share on other sites More sharing options...
wildteen88 Posted September 18, 2008 Share Posted September 18, 2008 You'll be better of using arrays rather than variables, after this is what arrays are used for. However to answer you question you can use variable variables: ${'lastQ'.$i} = ""; Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-644977 Share on other sites More sharing options...
PFMaBiSmAd Posted September 18, 2008 Share Posted September 18, 2008 I'll echo what wildteen88 has just stated, use an array any time you have a set of same type data, that is what arrays are for. Don't use a sequential list of variables. Your code will be simpler and faster (variable variables are three times slower than using an array.) Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-644979 Share on other sites More sharing options...
.josh Posted September 18, 2008 Share Posted September 18, 2008 And to make an array you would just do this: <?php $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; $lastQ[] = ""; ?> You can put a number (or string) inside the []'s to specify the array keys but if you leave it blank it will auto-generate a numerical key starting at zero then one then two etc... dunno what your actual data is but would be more efficient to loop it. Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-644981 Share on other sites More sharing options...
The Little Guy Posted September 18, 2008 Share Posted September 18, 2008 or... for($i=0;$i<20;$i++){ eval('$myVar'.$i.' = "'.$i.'";'); } echo $myVar4; Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-644989 Share on other sites More sharing options...
PFMaBiSmAd Posted September 18, 2008 Share Posted September 18, 2008 Ummm. eval() would be the slowest way possible, because the string of code must be parsed, tokenized, and then interpreted by the php language engine, rather than just using a simple assignment statement - $lastQ[] = ""; Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-644996 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 I agree. The array option would be best. However, if you have a real need to have the variable named like that, rather than in an array, the option I listed would be quick, too. I recently had a need to do that. My company launched a new web server. The new one doesn't assume variable names when a $_REQUEST is sent. For example, if you passed "?id=8" to a page on the old server, it would generate both $_REQUEST['id'] = 8 and $id = 8 automatically. Since the new one only creates the former, but so many apps require the latter, I had to add this: <?php foreach ($_REQUEST as $k=>$var) ${$k} = $var; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-645002 Share on other sites More sharing options...
The Little Guy Posted September 18, 2008 Share Posted September 18, 2008 Ummm. eval() would be the slowest way possible, because the string of code must be parsed, tokenized, and then interpreted by the php language engine, rather than just using a simple assignment statement - $lastQ[] = ""; Really? Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-645003 Share on other sites More sharing options...
PFMaBiSmAd Posted September 18, 2008 Share Posted September 18, 2008 F1Fan, what you are describing is a register_globals issue and the code you posted has the same security problems as register_globals did, allowing external data to overwrite your program variables. If your page had a variable called $admin that indicated the visitor was an administrator that was determined and set at the top of your script and then you had the code you just posted, all I would need to do is visit your page with "?admin=1" on the end of the URL and I would become an administrator and could do anything on your site that an administrator could? Do you really want that possibility to exist in your code? You need to directly use and reference only the exact variable names that you expect data to be in. Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-645013 Share on other sites More sharing options...
benphp Posted September 18, 2008 Author Share Posted September 18, 2008 Thanks everyone! I went with Wildteen88's suggestion: ${'lastQ'.$i} = ""; It sure cleared up a lot of copy/pasting. I'm using it for a questionnaire, where each question is set up as an array $q[1], $q[2], etc. Each of the answers is captured in MySql as q1, q2, etc. So being able to refer to a variable by number is a great help! Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-645063 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 I realize the security issues in this, and thankfully this is a secure in-network server, and it's just temporary while I go through all the code and fix it. Thanks for the info though. Quote Link to comment https://forums.phpfreaks.com/topic/124847-solved-how-to-automatically-generate-variable-names/#findComment-645079 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.