What does $$ (dollar dollar or double dollar) means in PHP ?

The $x (single dollar) is the normal variable with the name x that stores any value like string, integer, float, etc. The $$x (double dollar) is a reference variable that stores the value which can be accessed by using $ symbol before $x value.
From the given figure below, it can be easily understood that:

  • $x stores the value “Geeks” of String type.
  • Now reference variable $$x stores the value “for Geeks” in it of String type.

So, the value of “for geeks” can be accessed by two ways which are listed below:

  • By using Reference variable directly. Example: echo $$x;
  • By using the value stored at variable $x as a variable name for accessing the “for Geeks” value. Example: echo $Geeks; which will give output as “for Geeks” (without quote marks).

Examples:

Input : $x = "Geeks";  
        $$x = for Geeks;  
        echo "$x ";  
        echo "$$x;";   
        echo $Geeks;
Output : Geeks 
         for Geeks
         for Geeks

Input : $x = "Rajnish";  
        $$x = "Noida";  
        echo "$x lives in " . $Rajnish;
Output : Rajnish lives in Noida

Below examples illustrate the use of $ and $$ in PHP:

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Declare variable and initialize it
$x = "Geeks";     
  
// Reference variable
$$x = "GeeksforGeeks";
  
// Display value of x
echo $x . "\n"
  
// Display value of $$x ($Geeks)
echo $$x . "\n"
  
// Display value of $Geeks
echo "$Geeks";
  
?>

chevron_right


Output:

Geeks
GeeksforGeeks
GeeksforGeeks

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Declare variable and initialize it
$var = "Geeks"
  
// Reference variable
${$var}="GeeksforGeeks"
  
// Use double reference variable
${${$var}}="computer science";
  
// Display the value of variable
echo $var . "\n"
echo $Geeks . "\n"
echo $GeeksforGeeks . "\n"
  
// Double reference
echo ${${$var}}. "\n"
  
?> 

chevron_right


Output:

Geeks
GeeksforGeeks
computer science
computer science


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.