PHP | $ vs $$ operator

The $ operator in PHP is used to declare a variable. In PHP, a variable starts with the $ sign followed by the name of the variable. For example, below is a string variable:

$var_name = "Hello World!";

The $var_name is a normal variable used to store a value. It can store any value like integer, float, char, string etc. On the other hand, the $$var_name is known as reference variable where $var_name is a normal variable. The $$var_name used to refer to the variable with the name as value of the variable $var_name.

Examples:



Input :  $Hello = "Geeks for Geeks"
         $var = "Hello"
         echo $var
         echo $$var
Output : Hello
         Geeks for Geeks

Input :  $GFG = "Welcome to GeeksforGeeks"
         $var = "GFG"
         echo $var
         echo $$var
Output : GFG
         Welcome to GeeksforGeeks

Explanation: In the above example, $var stores the value “Hello”, so $$var will refer to the variable with name Hello i.e., $Hello.

Below program will illustrate the $ and $$ operator in PHP.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Variable declaration and initialization 
$var = "Hello";
$Hello = "GeeksforGeeks";
  
// Display the value of $var and $$var
echo $var . "\n";
echo $$var;
  
echo "\n\n";
  
// Variable declaration and initialization 
$var = "GFG";
$GFG = "Welcome to GeeksforGeeks";
  
// Display the value of $var and $$var
echo $var. "\n";
echo $$var;
?>

chevron_right


Output:

Hello
GeeksforGeeks

GFG
Welcome to GeeksforGeeks


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.