Downloads

 

Variable Variables

It is a rare occurrence that I find an excuse to use variable variables, but when that situation arises they are not only helpful but also fun in a way. I have often heard beginning programmers say that they find the concept difficult to grasp. I believe the confusion doesn’t stem from understanding the concept itself; but from understanding when a variable variable would be beneficial. In this article I will attempt to dispel the myth by including real-world examples along with the basic information about variable variables.

One thing that should be mentioned before we begin, variable variables should be used sparingly. Their overuse can make scripts difficult to debug and confusing six months down the road when you decide to update or add functionality to your code. So before you use them make sure the problem your trying to solve warrants their use. (Hopefully the examples used in this article will give you the background to make that decision.)

We will begin by explaining what variable variables are. First let see what the PHP manual says a variable variable is:

PHP Manual:
A variable variable takes the value of a variable and treats that as the name of a variable.

Let’s see if we can add a little more to it… A variable variable, is created when two dollar signs ($$) are placed at the beginning of a variable name. The PHP engine interprets this to mean the value of the variable (which has two dollar signs in front of it) is the name of the variable which needs to be interpreted.

Not sure if that is any clearer, so lets go to some code so we can see it in action, then I’m sure everyone will understand.

<?php
$site = "NULL";
$ring = "NULL";
$plug = "NULL";
 
$entity_1 = "site";
$entity_2 = "plug";
$id_1     = 54;
$id_2     = 78;
 
$$entity_1 = $id_1;
$$entity_2 = $id_2;
 
echo "site: ".$site."<br />";
echo "ring: ".$ring."<br />";
echo "plug: ".$plug."<br />";
?>

When this code is run it will print:
site: 54
ring: NULL
plug: 78

Notice the two dollar signs ($$) in front of the second occurrence of the variables ‘entity_1′ and ‘entity_2′, these are the variable variables. Since the value of the variable $entity_1 is ’site’ when PHP parses:

$$entity_1 = $id_1;

it reads:
$site = $id_1;
The same also happens for $$entity_2.

That’s it! That is what a variable variable is, nothing really ground breaking here. So the question is when should this feature of PHP be used?

First I have to admit that I have never run into a situation where variable variables are required to get the job done. I think it is very telling when the section on variable variables in the PHP Manual starts with the sentence:

Sometimes it is convenient to be able to have variable variable names.

The keyword here being: convenient (as in ‘not necessary’).

The most recent situation where I used variable variables was this:
I was writing a function which managed the data in a database correlation table. The table had three columns: ’site_id’, ‘ring_id’, and ‘plug_id’.

The function I was working on accepted the entity types and ids for two entities. Hence, the function declaration looked like this:

function correlate($entity_1, $id_1, 
                   $entity_2, $id_2)

That function used another function which was responsible for actually inserting a record into the table and its declaration looked like this:

function create($site_id,
                $ring_id,
                $plug_id)

The purpose of the function I was writing was to determine if a record existed for either entity in the db table, if so update the correlation, if not insert a new record. Variable variables came into play if a record needed to be inserted into the correlation table.

Once it was determined that a new record need to be inserted, I created three variables set to the column defaults (NULL):

function correlate($entity_1, $id_1, 
                   $entity_2, $id_2)
{
    //check for record
    if ($record) {
      //update and return
    }
 
    $site = "NULL";
    $ring = "NULL";
    $plug = "NULL";
}

Without some checking the function did not ‘know’ which ids it had. So to make things easier I decided to use variable variables and write the rest of the function like this:

function correlate($entity_1, $id_1, 
                   $entity_2, $id_2)
{
    //check for record
    if ($record) {
      //update and return
    }
 
    $site = "NULL";
    $ring = "NULL";
    $plug = "NULL";
 
    $$entity_1 = $id_1;
    $$entity_2 = $id_2;
 
    return create($site, $ride, $plug);
}

Again variable variables were not needed in this situation I could have used ’switch’ or ‘if’ statements to determine which entity types were sent, but I thought the use of variable variables was a cleaner option and quite frankly was faster to code.

BTW – If anyone has run into a situation where variable variables were required, please let me know I would love to hear about it.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Improve the web with Nofollow Reciprocity.