Variables Variable in PHP – Double Dollar ($$) Variable

In this trick, we will learn what is variables variable in PHP or you can say double dollar ($$) or references variable.

The $x single dollar is a normal variable in PHP where x is the name of the variable and you can hold the value in this variable.

And the $$x double dollar is a reference variable that stores the value which means the $ symbol holds the value of $x and you can access it by adding the $ prefix of the original variable ($x).

Variable’s Variable in PHP – Double Dollar ($$)

Using the double dollar ($$) variable, you can access the value of other reference variables. The syntax of using the double dollar variable ($$variable) is also called the variables variable in PHP.

Let’s see how we can use it.

Output
YourBlogCoach

Here is the explanation:

  • The $original_variable variable hold the text “YourBlogCoach
  • Then $name contain the name of the variable “original_variable
  • So $$name means that the variable that has its name contained in $name and that reference is $original_variable which has the value text.

Let’s take another real-world example, which gives you a better understanding of using the double dollar reference variable in PHP.

Output
Mohit study in Computer Science

Being Tricky 😉

Leave a Reply

Your email address will not be published. Required fields are marked *