Open In App

Difference between $var and $$var in PHP

Last Updated : 18 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, $var is used to store the value of the variable like Integer, String, boolean, character. $var is a variable and $$var stores the value of the variable inside it.

$var:

Syntax:

$variable = value;
  • The $variable is the variable name
  • The value is the initial value of the variable.

Example 1: This example stores and displays values with $.

PHP
<?php 

       // String value
       $value1 = "hello Geeks";  
 
    // Display string value
    echo $value1; 
    echo "<br/>";

    // Boolean value
    $value2 = true;       
    
    // Display boolean value
    echo $value2;  
    echo "<br/>";

    // Integer value
    $value3 = 34;    

    // Display integer value
    echo $value3; 
    echo "<br/>";
?>
 

Output
hello Geeks<br/>1<br/>34<br/>

$$var: $$var stores the value of $variable inside it.

Syntax:

$variable = "value";  
$$variable = "new_value";
  • $variable is the initial variable with the value.
  • $$variable is used to hold another value.

We can get another value by using the $value of the first variable.

Example 2: PHP program to demonstrates $$var.

PHP
<?php 

      // String value
      $value1 = "hello";  

       // Display string value
      echo $value1; 

      echo "\n";

      // Store another string in $$var
      $$value1 = "Hello php";

    // Access another string using
    // value of $var
      echo "$hello";
?>

Output
hello
Hello php

Difference between Both: The variable $var is used to store the value of the variable and the variable $$val is used to store the reference of the variable.


Next Article

Similar Reads