PHP $ and $$ Variables

In this lesson, you will learn about using the $ single dollar sign and $$ double dollar sign in PHP and examples to understand the topic better.


PHP $ Variable

A variable with a single dollar sign $ in PHP stores any value, including strings, integers, floats, and other data types.


PHP $$ Variable

On the other hand, The reference variable $$ (double dollar) stores the variable’s value inside it. To understand the difference better, let’s see an example.

Example

<!DOCTYPE html>
<html>
<body>

<?PHP
// Difference between $ and $$
$Variable = "Standard_Variable";
$$Variable = "This is a reference variable";
echo $Variable . "<br/>";
echo $$Variable . "<br/>";
echo $Standard_Variable;

?>
</body>
</html>

Output

Standard_Variable
This is a reference variable
This is a reference variable

The variable “Variable” has the value MyString_Variable in the above example. By assigning the value “This is a reference variable” to the reference variable $$Variable, we have printed the values $Variable, $$Variable, and $MyString_Variable.

This concludes the $ and $$ Variables lesson. In the next lesson, you will learn about another important keyword in PHP, the constants!