PHP $ And $$ Variables - PHP $var
is a normal variable that is used to store string, integer, float, etc.
$$var
is known as reference variable that is used to store $var value inside $variable
. Let us take an example to understand it.
Source Code
<?php
$fruit="mango";
$$fruit="orange";
echo $fruit."<br/>";
//mango
echo $$fruit."<br/>";
//orange
echo $mango;
//orange
?>
Code Explanation: Here $fruit
value will be "mango"
while $$fruit
can be written as $mango
so $$fruit
as well as $mango
both values will be "orange".
Source Code
<?php
$city="mumbai";
$$city="delhi";
${${$city}}="gorakhpur";
echo $city. "<br>";
//output:mumbai
echo $$city. "<br>";
//output:delhi.
echo $mumbai;
//output :gorakhpur
?>
Code Explanation:
variable $city
hold value ="mumbai".
variable ${$city}
hold value ="delhi" // it also declare like ${mumbai}
.
variable ${$ {$city} }
hold value="gorakhpur" // it act as "variable's of variable of variable" reference.