Difference Between $var and $$var in PHP


$$var uses the value of the variable whose name is the value of $var. It means $$var is known as reference variable where as $var is normal variable. It allows you to have a “variable’s variable” – the program can create the variable name the same way it can create any other string. .

<?php
$name="Sam";
$$name="Dam";
echo $name."<br/>";
echo $$name."<br/>";
echo $Sam;
?>


Output
Sam
Dam
Dam
In the above example $name is just a variable with string value=”Sam”. $$name is reference variable . $$name uses the value of the variable whose name is the value of $name. echo $name print the value: Sam echo $$name print the value:Dam \ value of this($name) variable is act as reference of second variable($$name). echo $Sam print the value :Dam \ Here $Sam is also act as reference variable.

Comments