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:
It retrieves the string “mouse” that $animal is pointing to.
It prepends a dollar sign to the string: $”mouse”
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.
$breed contains the string German_Shepherd.
PHP creates $German_Shepherd ($$breed).
$German_Shepherd contains the string Canis_Lupus_Familiaris.
PHP creates $Canis_Lupus_Familiaris ($$$breed).
$Canis_Lupus_Familiaris contains the string Canis_Lupus.
PHP creates $Canis_Lupus ($$$$breed).
$Canis_Lupus contains the string Canis.
PHP creates $Canis ($$$$$breed).
$Canis contains the string Canidae.
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.