PHP $ and $$ Variables

Assigned by $ sign variable is normal variable and $$ sign variable is reference variable. Single dollar sign ($) assigned variable ($str) stores any value like string, integer, float, data, char, double etc. Double dollar sign ($$) assigned variable ($$str) stores value of the $str inside it.

PHP $Syntax:
$str=value;

PHP $$ Syntax:
$$str=value;

PHP Example :

Look at below example, $str variable prints xyz,  $$str variable prints 500 and $xyz variable prints 500. We have assigned $str variable value to $$str which becomes variable $xyz.
<?php  
    $str = "xyz";  //single dollar normal variable
    $$str = 500;   // double dollar reference variable
    echo $str."<br/>";  // prints value of variable $str  
    echo $$str."<br/>"; // prints value of double dollar variable
    echo $xyz;  // prints value of $$str variable
?> 

Output:

xyz
500
500

Comments

Leave a Reply

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

68926