Variable variables $$var in php

PHP allows us to declare variable variables using double dollar ($$).

$$ is used to declare dynamic variables and assigning values to it.
Variable variables allow you to access the contents of a variable without knowing its name directly – it is like declaration of  dynamic variables  and assigning values. Consider this piece of code:

$a = ‘b’;
$$a = 3;
echo $b; // outputs : 3

in the above example value of $a (b) becomes a variable ($b) and its value is assigned as 3.

In this way we can create dynamic variables using variable variables ($$).

Question for visitors :

What is the out put of below code :

$f= 10;
$b= “f”;
$w= “b”;
$t= “w”;
print $f;
print $$b;
print $$$w;
print $$$$t;

One thought on “Variable variables $$var in php

Leave a Reply

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