PHP $ And $$ Variables

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	
?>
        
Try it now

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".

Example 2

Source Code

          <?php  
$city="mumbai";  
$$city="delhi";  
${${$city}}="gorakhpur";
echo $city. "<br>"; 
//output:mumbai 
echo $$city. "<br>"; 
//output:delhi. 
echo $mumbai;
//output :gorakhpur
?>
        
Try it now

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.

Web Tutorials

PHP $ And $$ Variables
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4