picture of Robert
During a recent project I discovered a totally new concept in programming for myself, variable variables. I needed to update multiple records from one page. After banging my head on the wall for quite some time, I stumbled across the variable variable and all my troubles went away!

Introduction

So, just what the heck is a variable variable anyway? According to the PHP manual, a variable variable takes the value of a variable and treats that as the name of a variable. While this may seem pretty straightforward, it can be a little confusing with so many uses of the word "variable" in one sentence. What the definition simply states is that you can set a variable -- x equals this -- and then set the variable variable, meaning you set the value of x as a new variable of x. In this example, the variable this would equal is cake. In PHP, this would be denoted as follows:

<?php

$x 
"this";
$
$x "is cake";

?>
The $$ is the syntax used in PHP for a variable variable. I can now call the two variables $x and $$x two ways.

<?php

echo "$x ${$x}";

?>

 

<?php

echo "$x $this";
    
?>
Both of these will return the string "this is cake". Notice that $$x is written as ${$x} in echo. This lets PHP know that you are using the variable $$x and not $ and $x.
Still scratching your head? Or maybe you get it, but want a more in depth and actually useful example? In the next section I will show you how variable variables may be used to edit multiple records on one page.

Example

Let's say you have a MySQL database that stores link submissions for sites of interest. In that database there is a table called submissions. The fields in the submissions table are as follows:
	SubmissionID
	PostedBy
	Link
	Description
	Approved
You want to be able to display all the submissions that have been made but are not yet approved in a table. The editor will then be able to correct any typing mistakes, select the proper radio button next to each link for approval, and submit them all at once.
First, when you pull the data from the database and display it on the page you must set each name for every record to be unique. This will enable us to loop through the records and be able to identify what value goes where once the submit button has been pressed. To do this we do the following:

<?php

//Initialize counter variables

$index 0;
$index_count 0;

echo 
"<form method=post action=$PHP_SELF>\n";
echo 
"<table>\n";
echo 
"<tr><td><b>Posted By</b></td><td><b>Link</b></td>".
    
"<td><b>Description</b></td><td><b>Approved</b></td></tr>\n";


/*
Assuming we already have retrieved the records from the database into an array setting 
$myrow = mysql_fetch_array().  The do...while loop assigns a value to the $xstr variable 
by taking the name and concatenating the value of $index to the end starting with 0.  So 
the first time through the loop $SubmissionIDStr would have a value of SubmissionID0 the 
next time through it would be SubmissionID1 and so forth.
*/

do {

$SubmissionIDStr SubmissionID.$index;
$PostedByStr PostedBy.$index;
    
$LinkStr Link.$index;
    
$DescriptionStr Description.$index;
    
$ApprovedStr Aprroved.$index;


//This section would print the values onto the screen one record per row

printf("<tr><td><input type=hidden name=%s value=%s><input type=text name=%s value=%s></td>
<td><input type=text name=%s value=%s></td><td><input type=text name=%s value=%s></td>
<td><input type=radio name=%s value=-1>Yes<input type=radio name=%s value=0 checked>No</td></tr>\n"

$SubmissionIDStr$myrow["SubmissionID"], $PostedByStr$myrow["PostedBy"], $LinkStr$myrow["Link"], 
$DescriptionStr$myrow["Description"], $ApprovedStr$ApprovedStr);


//Increase counter values by 1 for each loop

$index++;
$index_count++;

} while (
$myrow mysql_fetch_array($result));

// I also had to create an index count to keep track of the total number of rows.

echo "<INPUT TYPE=hidden NAME=counter VALUE=$index_count>\n";

echo 
"<INPUT TYPE=submit></form>\n";

?>
After the submit button is pressed to we have to loop through all the variables on the page again. We are able to do this with the $index_count variable we created. Next, we assign the value of those variables to yet another variable in the second step. This is where variable variables come into play.

<?php

//This loops through all the records that have been displayed on the page.

for ($index 0$index <= $counter$index++) {

    
    
/*
    This part sets a variable with the names we created in the first section.  
    We start with 0 and go until the number saved in the $index_count variable.
    */ 
    
    
$varSubmissionID 'SubmissionID'.$index;
    
$varPostedBy 'PostedBy'.$index;
    
$varLink 'Link'.$index;
    
$varDescription 'Description'.$index;
    
$varApproved 'Approved'.$index;

    
    
/*
    This is the variable variable section.  We take the value that was assigned 
    to each name variable.  For example the first time through the loop we are 
    at the record assigned with SubmissionID0.  The value given to SubmissionID0 
    is set from the first section.  We access this value by taking the variable 
    variable of what SubmissionID0 is.
    */

    
$SubmissionIDvalue = $$varSubmissionID;
    
$PostedByvalue = $$varPostedBy;
    
$Linkvalue = $$varLink;
    
$Descriptionvalue = $$varDescription;
    
$Approvedvalue = $$varApproved;


    
//Update the database

    
$sql "UPDATE submissions SET PostedBy='$PostedByvalue',Link='$Linkvalue',".
        
"Description='$Descriptionvalue' WHERE SubmissionID=$SubmissionIDvalue'";
    
$result mysql_query($sql);


    
//If the link was marked approved set the value of the Approved field

    
if ($Approvedvalue == '-1') {
        
$sql "UPDATE submissions SET Approved='-1' WHERE SubmissionID=$SubmissionIDvalue";
        
$result mysql_query($sql);
    }

}

?>
I hope this has helped you to understand the basics of variable variables and given you some ideas of how to use them in future projects. They were pretty confusing to me at first, but once you get the basic understanding of how variable variables work they're a piece of cake. Let me know if you have any other questions.
--Robert