This section describes what is a variable variable name, storing the variable name in another variable, naming variables with expressions, using any expressions as variable names.
What is a Variable Variable Name:
A variable variable name is a variable name that is stored inside another variable.
Normally, a variable name is a static string. But sometimes, you want to store a variable name inside another variable
to be able to participate in operations.
PHP supports variable variable names with following basic rules:
1. If the name of the original variable is stored in a different variable, you can use the $$var notation to identify
the original variable in your PHP source code. For example, $variableName = "title"; $$variableName represents
the variable $title.
2. If the name of the original variable can be calculated by an expression, you can use the ${exp} notation to identify
the original variable in your PHP source code. For example, $variableName = "title"; ${$variableName."_1"} represents
the variable $title_1.
3. Variables with variable names can be assigned with new data like normal variables. For example,
$variableName = "title"; $$variableName = "Herong's PHP Book"; assigns "Herong's PHP Book" to $title.
4. Variables with variable names can be used in any operations like normal variables.
$variableName = "title"; $$variableName = "Herong's PHP Book"; print($$variableName." is free"); prints "Herong's PHP Book is free".
5. If you use an expression as the variable name, the result of the expression will be automatically converted to a string,
which will be used as the variable name. With this automatic conversion, anything can be used as a variable name.
Even an empty string can be used as a variable name, ${""} is a perfect variable expression.
Now let's look at my tutorial example below to confirm some rules mentioned above: