There are times when it’s convenient to be able to dynamically name and assign variables, but how do we go about doing so?
PHP syntax provides for the ability to do this with a feature called “variable variables.” A quick example is below.
/*Lets set some variables.*/ $char1 = 'Kirby'; $char2 = 'March'; $character = 'char2'; echo $$character;
The code above will print out March because PHP sees the two dollar signs right
after each other, and then knows to that it needs to print out the value for $char2 since $character contains a string
called char2. Additionally, we can use this to set variables as well:
$character = 'char3'; $$character = 'Salsa'; echo $$character;
Now a variable named $char3 exists and we’ve set it to contain the name ‘Salsa’ without ever needing to actually write
“$char3 = ‘Salsa’;.”
Pretty nifty eh? This is just a basic primer, if you’re ready to see a more in-depth guide, read the PHP manual on the subject.