Learn how PHP's variable variables allow you to change the variable name dynamically on the fly, which can be useful when the same code needs to be applied to different elements.
- [Narrator] Hi, I'm David Powers and welcome to this week's edition of PHP Tips, Tricks and Techniques, designed to help you become a smarter, more productive PHP developer. This time, I'm going to look at a powerful concept known as variable variables. Yes, they really exist, I'm not just repeating myself. With an ordinary variable, the name of the variable is always the same. It's only the value that ever changes. With a variable variable, on the other hand, it's not only the value that can change, the name of the variable can also be changed dynamically.
This can be useful in situations where the same code needs to be executed over and over again by different variables. So how do you create a variable variable? Well, you start with an ordinary variable. Let's call it a. Assign it a string as it's value. This will be used as the variable variable's name. So it can't contain any spaces or special characters other than an underscore and it can't begin with a number. You create a variable variable from a by preceding it with a second dollar sign and assign equal to value.
This can be any data type. Doing so has the effect of creating an ordinary variable called hello with the same value. The string assigned to the ordinary variable a has in effect, become the name of the variable variable. Well, so much for the theory. Let's switch to my editing program to see a practical use for variable variables. This file contains a multi-dimensional associative array with the name, age and gender of several people.
And the names of the men and women are mixed up. I want to separate them into different arrays, according to gender. So I've created two separate arrays, one for men, the other for women. Then here on line 14, I've initialized two counter variables, m and w to zero. Then this for reach loop goes through the member's array and as it goes through each sub-array, if the value of gender is m, name and age are assigned to the men array, otherwise they're assigned to the women array.
Then down at the bottom here, we display the results using print_r. So, if I run this script by loading into a browser, there's the result. At the top we've got the men array, Bob, Eddy and Frank. And then we've got the women's array. Let's take a look at the code in this for reach block. Apart from the variables used for the surface arrays and counters, the code in both blocks in this conditional statement is identical. So instead of repeating the code, this is a case where we could use variable variables.
So let's switch to this other file. It contains the same basic script but I've cleared out the code inside that conditional statement. So if gender is m, we'll create an ordinary variable called array and assign it the string men. And we'll create a counter variable called i and assign it the string lowercase m. And then in the else block, we'll do pretty much the same except that array will be women and i will be w.
Then after this conditional statement, we can use array and i as variable variables, remembering to precede them with two dollar signs each time. So $$ array and we need the counter, that'll be $$i and then we'll have the name element. And we'll assign that the value member name.
And on the next line, $$array, add in square brackets, $$i, our counter and we need to increment it so plus, plus and then we'll have the age. So that will be assigned member age. As the loop goes through each member sub array, if gender is m, this array of variable variable will become men.
And the i variable variable will become m. But if gender is w, the variable variable array will become women and the variable variable i will become w. And we can check that by running this script by loading it into a browser and there is the result. It's exactly the same as before. Now I imagine that some of you might be thinking, but we've ended up with a script that's longer than the original and in this case, that's true.
But I've kept the script deliberately simple. You might be doing a lot more work with each sub array rather than these two lines. Also using variable variables ensures that each sub array is processed the same way. With the previous code, the danger you might make a change to the code in one block but forget to update it in the other one. So that's how you use variable variables. They're powerful because the variable name can be changed dynamically. The dynamic name is inherited from an ordinary variable that must have a string as it's value.
Because it's used as the dynamic name for the variable variable, the string can contain only alpha numeric characters and the underscore and it can't have any spaces. In our practical example, we assigned the string men to an ordinary variable called array. Preceding array by a second dollar sign created a variable variable that acted like an ordinary variable called men. But when the value of the ordinary variable was changed to women, the variable variable acted like an ordinary variable called women.
This concept can be difficult to grasp at first but it can be useful in situations where the same task needs to be performed with different variables. Well, that's it for this edition of PHP Tips, Tricks and Technique. Until next time, thanks for watching.
Note: The exercise files are free to all members. The code is commented to enhance your learning, but you will need database connectivity for some files to run as intended.