Jump to content

[SOLVED] How to automatically generate variable names?


Recommended Posts

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!

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.)

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.

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[] = "";

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;
?>

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?

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.

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!

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.