Going back to basics is sometimes the best way, to discover new things. When first learning PHP or having worked with it a lot over the years, it's easy to forget the complete depth of the programming language. There is so much you can do with the language and it's easy to use the same techniques over and over. Understanding variables, basic syntax, and predefined variables (including variable scope) are all things you'll pick up early into your PHP journey. In this guide, you'll explore variable variables in PHP, what they are, and how they work.
What Are Variable Variables in PHP?
In PHP, it's common to have many, many variables in your code. They can store information in various formats, such as strings, arrays, or integers. In your code, you'll give that variable a name. The name is used to help you (and more importantly) others, understand your code and what it's doing. Having bad or confusing naming conventions in bad PHP and programming practice. It's best to make sure your variable names are as descriptive as possible. With this in mind, it's also possible there might be times when you want to create a variable dynamically. In the standard format (a PHP variable not a 'variable variable'), that's not possible because you the coder have to define the variable before you run the script. However, variable variables allow you to create variables at runtime! That means the variable could named be as anything at runtime and be useful for the job at hand. They can be used for class properties, and arrays in PHP.
What's a normal variable?
A 'normal' variable in PHP starts with a dollar sign '$' followed by a name (string). In the PHP code example below, this is a standard variable, called 'hello', with the string value of 'world'.
$hello = 'world';
How to declare a 'variable variable'?
Now let's see how to declare a variable as that variable in PHP. To declare variable variables you can use a double dollar sign '$$'. This tells PHP that you want the previous variable's value to be the name of the new variable you are creating. With our new dynamically creating variable, we can just like a normal variable, assign a value to it, in this case, '123'. Now at runtime, PHP has available, variables 'hello' and variable 'world'.
# First we have the same normal variable as before
$hello = 'world';
# Which outputs, world.
echo ($hello);
# Secondly we're now using a double dollar sign to create a 'variable variable'
$$hello = '123';
# Which outputs, 123.
echo ($world);
It's also possible to create even more variable variables, which contain multiple dollar signs, but this is uncommon in general PHP. The reason up to two dollar signs is considered acceptable, are you only need to find the parent variable to understand what is doing on. Using more than this becomes unreadable and hard to follow.
# Standard variable
$hello = 'world';
# A 'variable variable'
$$hello = '123';
# Triple dollar signs ('variable variable variable') and beyond are uncommon in PHP
$$$123 = '456';
In the examples already shown, the double dollar variables are already set, therefore not making them truly dynamic. Let's look at an example where the variable variables aren't defined until runtime occurs. In the code example below, the days of the week variables don't exist until the for loop ('foreach') has run. Here we are using a combination of 'strtolower', 'date', and 'strtotime'. The function strtotime is converting a string time into an int for use by the date function and because the date function returns a day of the week with a capital letter, we're then converting that string to all lowercase with strtolower. The variables (days of the week), are then available to us to use later in our code.
$daysOfWeek = [1,2,3,4,5,6,7];
foreach($daysOfWeek as $day) {
$dayName = strtolower(date('l', strtotime("2030-04-" . $day)));
$$dayName = $day;
}
echo 'Monday is day: ' . $monday . "\n";
echo 'Tuesday is day: ' . $tuesday . "\n";
echo 'Wednesday is day: ' . $wednesday . "\n";
echo 'Thursday is day: ' . $thursday . "\n";
echo 'Friday is day: ' . $friday . "\n";
echo 'Saturday is day: ' . $saturday . "\n";
echo 'Sunday is day: ' . $sunday . "\n";
# Outputs
Monday is day: 1
Tuesday is day: 2
Wednesday is day: 3
Thursday is day: 4
Friday is day: 5
Saturday is day: 6
Sunday is day: 7
When using variables variable with arrays, it's easy to get stuck with the ambiguity problem. That is, the parser will need to know which variable you wish to use in the context of an array. Consider the below example, when writing '$$test[1]', did you want it as a variable with index one, the double dollar variable, or the index from that variable? To overcome this you can use curly braces to clearly tell the parser what you want.
# When writing this
$$test[1];
# Do you want
$test[1];
# Did you actually want double the dollar with the index value?
$$test;
$$test[1];
# Instead use curly braces
${$test[1]};
${$test}[1];
Conclusion
Using the variables variable method in PHP is a different approach when it comes to setting and using your variables in PHP.
- Variables variable cannot be used in PHP's superglobal arrays
- They cannot use the "$this" variable as this is a PHP-reserved special variable in OOP (object-oriented programming) context
- Be careful not to nest the variables too deep and stick to a maximum of double dollar signs ('$$')
- Watch out for the ambiguity problem when using variables variable with arrays and use curly braces to explicitly tell the parser what you want