Thursday, October 9, 2025
HomeLanguagesPHP | $ vs $$ operator

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 neveropen"
         $var = "GFG"
         echo $var
         echo $$var
Output : GFG
         Welcome to neveropen

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.




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


Output:

Hello
neveropen

GFG
Welcome to neveropen
RELATED ARTICLES

Most Popular

Dominic
32348 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS