Variable Variables in PHP

In PHP, you can name a variable with the value stored in another variable. That is, one variable contains the name of another variable.
Nov 08, 2019 — 3 mins read — Programming

Variable Variables in PHP


In PHP we are allowed to name a variable with the content of another variable. These are called variable variables and can be used at any point in the code execution. Let’s look into a few examples of how we can use them

// ex.1
$foo = "bar";
$bar = "some";
echo $$foo;

In the first example, we have a variable called “foo” with the content of “bar”, and a variable “bar” with the content of “some”. To create or reference a dynamic variable, we will use the content of the “foo” variable and by adding an extra dollar sign we theoretically referenced the “bar” variable. If we now look into the output we can see that the content of the “bar” variable is printed.  

// ex. 2
$list = [
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'address' => 'Some Address, Somewhere'
];
foreach ($list as $k => $value) {
    $$k = $value;
}
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $address;
echo "<br>";

The second example is a more practical one, where we first have a list of key-value pairs. We can then loop through all of them and use a variable variable to extract each of the array keys as its own variable. When that is done, we can reference and print each of the keys directly with the new variables created. This approach is very handy in situations where we need to print such variables in the HTML content. 

//ex. 3
$one = "two";
$two = "three";
$three = "four";
$four = "five";
echo $$$$one;

The interesting thing with variable variables is that we are not limited to one level reference. We can reference the content through multiple levels as in the third example. The output of this code will be five because each of the dollar signs references the content of the next variable. 



In some cases where we use variable variables, we can have a more complex expression so the two dollar signs one next to each other is not really readable. We can, however, use angle brackets to distinguish where pieces are. Additionally, when printing such variables in a string, we must use the angle brackets to let PHP know that we are using variable variables. If we don’t do that, the extra dollar sign will be printed as content regularly. In example 4, we are printing the same thing twice, first with the angle brackets notation and then with the variable variable created. 

//ex. 4
$a = 'hello';
$$a = 'world';
echo "$a ${$a}";
echo "<br>";
echo "$a $hello";
echo "<br>";

In example five, we are demonstrating that the name of the variable variable can be generated with any logic that we might come up with and to let PHP know to create it we must use the angle brackets notation. Here the string concatenation will first be evaluated and the resulting value will be used as a variable name overriding the value that we initially set.

//ex. 5
$variableName = "variable";
$variableNamePart1 = "vari";
$variableNamePart2 = "able";
${$variableName} = 5;
${$variableNamePart1 . $variableNamePart2} = 10;
echo $variable;
echo "<br>";

When using complex expressions with variable variables, there are some differences in the execution flow in PHP5 and PHP7 that you need to be aware of. In PHP7, all expressions are evaluated strictly left to right wherein PHP5, the evaluation was mixed. 

//ex. 6 - Differences between PHP5 and PHP7
/*
    Case 1 : $$foo['bar']['baz']
        PHP5 interpretation : ${$foo['bar']['baz']}
        PHP7 interpretation : ${$foo}['bar']['baz']
    Case 2 : $foo->$bar['baz']
        PHP5 interpretation : $foo->{$bar['baz']}
        PHP7 interpretation : $foo->{$bar}['baz']
    Case 3 : $foo->$bar['baz']()
        PHP5 interpretation : $foo->{$bar['baz']}()
        PHP7 interpretation : $foo->{$bar}['baz']()
    Case 4 : Foo::$bar['baz']()
        PHP5 interpretation : Foo::{$bar['baz']}()
        PHP7 interpretation : Foo::{$bar}['baz']()
*/

In case 1 the old behavior was that the entire expression was evaluated and then used as a name for the variable wherein PHP7, the content of the “foo” variable is used. Similarly in all the other cases, the dynamic variable is evaluated first before continuing with the rest of the expression. 



I hope that this article was interesting for you and that you managed to learn something out of it. To learn more in the future, make sure that you subscribe to my YouTube channel and check out some of my other videos. 

php tutorial
Read next

Fixing IKEA FIXA 7.2V Battery Drill That Won't Spin

In this article, we will repair an IKEA FIXA battery drill that starts spinning but it immediately stops with a bit of smoke coming out.&nbs...

You might also enojy this

Everything you need to know about switches and Arduino

In this article, we will look into the basics of switches and how we can use switches with an Arduino microcontroller in our projects. Stick...