What are Variable Variables in PHP?

In this article, we will learn how PHP dynamically declares a variable. That is reference variable also known as Variable Variables in PHP.

PHP allows the programmer to create and use the reference of an existing variable by making another variable out of that normal variable.

What is Variable in PHP

Variable stores data like integer, float, string and etc. In PHP, a variable is declared with the dollar($) symbol.




$variable=5; //creating an integer variable
echo "value of variable:".$variable;


value of variable:5


In PHP, the datatype of a variable is dynamic in nature i.e. datatype of the variable depends on what type of data is present in that variable.

Reference variable(variables) or Variable Variables in PHP

It is the reference to a normal variable i.e. it stores the value of a normal variable as the variable name. It is declared with the double dollar($$) symbol.





//declaring a variable

echo "Value in \$name_of_country:".$name_of_country."</br>";

//creating reference to another variable
echo "Value of the reference to \$name_of_country:". $$name_of_country."</br>";
echo "Value of \$India:".$India;//Same as $$name_of_country


Value in $name_of_country:India
Value of the reference to $name_of_country:WestBengal
Value of $India:WestBengal

Let us take another example to clarify the reference variable concept,


//declaring a normal variable
echo $name. "<br>"; 

//creating reference to normal variable
echo ${$name}. "<br>";  
echo $India. "<br>";  //Same as $$name

//creating reference to reference variable  
echo ${${$name}}. "<br>";  
echo $Wb. "<br>";  //Same as $$$name and $$India



From the above article, we can conclude that there is no limit for creating and accessing references from a normal variable.

If you have any doubts on the above article, leave a comment below.

See also,

Leave a Reply

Your email address will not be published. Required fields are marked *