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

Table of Contents
php php FAQs

Improve Article

Save Article

  • Last Updated :21 Jun, 2022

Improve Article

Save Article

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 the $ symbol before the $x value. These are called variable variables in PHP.

Variable Variables:- Variable variables are simply variables whose names are dynamically created by another variable’s 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.

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

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

  • By using the 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 GeeksInput : $x = "Rajnish"; $$x = "Noida"; echo "$x lives in " . $Rajnish;Output : Rajnish lives in Noida

Below are examples illustrating the use of $ and $$ in PHP:

Example-1:

php

<?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";

?>

Output:

GeeksGeeksforGeeksGeeksforGeeks

Example-2:

php

<?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";

?>

Output:

GeeksGeeksforGeekscomputer sciencecomputer science

My Personal Notesarrow_drop_up

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

FAQs

What is $$ variable means in PHP? ›

What is $$ variable means in PHP?

What is dollar vs double dollar in PHP? ›

What is dollar vs double dollar in PHP?

How much is $$ dollar in PHP? ›

How much is $$ dollar in PHP?

What does $$ mean in programming? ›

What does $$ mean in programming?

Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5822

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.