PHP Variable Variables

Variable Variables? Was that a typo?

Variable variables: no that’s not a typo. That’s what this topic is about. PHP variable variables are dynamic variables, meaning that you can generate variable names dynamically in PHP. How is this done? If you prepend a dollar sign to a string, the string will then become a variable.

Let’s create a regular variable $a. The variable $a will contain the string “bear.” We’ll then create another variable, and this time we’ll add an additional dollar sign before it. We’ll call the new variable variable $$a.

 

<?php
$a = "bear";
$$a = "cow";echo $a;
echo $$a;
?>

 

If we echo out both variables, PHP will see the first regular variable, $a, and will retrieve the data it’s storing. It contains the string “bear.” It evaluates the string and outputs it to the screen. It moves to the next statement and sees the variable variable $$a. It starts with the initial variable $a and retrieves the string “bear.” It then prepends another dollar sign to it, making a variable $bear. Technically $$a is $bear. So it retrieves the content that $bear is pointing to, which is “cow,” and outputs it to the screen.

I wanted to start off with an example that’s a little more challenging, just to get it out of the way and just to get the wheels turning. Let’s move on to a more practical example. Let’s create two variables again, $mouse and $animal. The variable $mouse will contain the string “Small” and the variable $animal will contain the string “mouse.”

 

<?php
$mouse = "Small";
$animal = "mouse";echo $animal;
echo $$animal;
?>

 

If we echo out $animal, PHP will see that it stores the string “mouse” and will output it to the screen. The statement that’s echoing out $$animal will be interpreted a little differently than the first statement. PHP will take the following steps:

  1. It retrieves the string “mouse” that $animal is pointing to.
  2. It prepends a dollar sign to the string: $”mouse”
  3. It evaluates the new variable $mouse and outputs the string “Small”

You’re not limited to one additional dollar sign.

 

 

We’ve created a bunch of variables, such as $breed, $German_Shepherd, $Canis_Lupus_Familiaris, etc. Using just the variable $breed, we can get all of the other variables that were created using variable variables. The first one is easy, breed. We just echo out $breed and we get the breed of the animal: German_Shepherd. The underscores are intentional; we’re creating variable variables and we cannot have spaces in variables. The naming convention follows the same rules.

To get the subspecies, we can prepend a dollar sign to $breed, and we’ll get the variable $German_Shepherd. The variable $German_Shepherd holds the subspecies string, which is “Canis_Lupus_Familiaris.” We can get the subspecies the same way, as well as all other variables on the list. Let’s jump down to Family. To get to the family variable, we’ll need to prepend 4 additional dollar signs to the $breed variable: $$$$$breed.

Let’s look at all of the variable variables that PHP will create for $$$$$breed.

  1. $breed contains the string German_Shepherd.
  2. PHP creates $German_Shepherd ($$breed).
  3. $German_Shepherd contains the string Canis_Lupus_Familiaris.
  4. PHP creates $Canis_Lupus_Familiaris ($$$breed).
  5. $Canis_Lupus_Familiaris contains the string Canis_Lupus.
  6. PHP creates $Canis_Lupus ($$$$breed).
  7. $Canis_Lupus contains the string Canis.
  8. PHP creates $Canis ($$$$$breed).
  9. $Canis contains the string Canidae.
  10. The string Canidae is echoed out to the screen.

You can keep appending dollar signs to get to the Kingdom. For this example, you’ll need to prepend 8 additional dollar signs to the $breed variable: $$$$$$$$$breed.

And those are variable variables for you. Sometimes this is beneficial when you’re retrieving items from the database. We won’t look into the details here, but know that there are reasons why this may be used.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Variables

More Math? x + 3 = 5 Kind of Variables?

PHP – P12: Variables

If you’re familiar with other programming languages, an associative array is pretty much a dictionary. Instead of accessing the array by using an index value, you’ll access it by using a key.

PHP Variable Variables

Variable Variables? Was that a typo?

PHP – P13: Variable Variables

PHP variable variables are dynamic variables, which mean that you can generate variable names dynamically. Prepend a dollar sign to a string, the string becomes a variable.

PHP Constants

WE’RE STARTING TO BE CONSISTENT.

PHP – P14: CONSTANTS

Constants are similar to variables except that they cannot change. They can’t change since constants are stored in the read-only-data segment.

Leave a Reply