PHP Variable Varibles Example


Variable variables are the special features of PHP in PHP. It supports to store the name of a variable into a variable. It is used to change the variable name dynamically.

Accessing Variables

Variable Variables are start with double dolor($$) sign. It can be used in tree like access, {variable variables name} -> {variable name} -> {variable value}. For example,

The $$category is the variable variable in this example. PHP will execute this with the steps, $$category -> $ { $category } -> $language -> PHP.

Accessing Class Properties using Variable Variables

We have an example to access class properties using PHP variable variables.

Accessing Array Values

We can use PHP variable variables in arrays. Sometimes we require to separate variable variables with curly braces to avoid confusions. We have examples to see what will happen if don’t use curly braces in PHP variable variables.

${$entertainment[0]} will return $m which is not found and cause undefined variable error as,

To resolve this error, we need to change the code as,

This is an example to access class property containing array, using variable variables with curly braces,


Leave A Reply

Your email address will not be published.