pTutorial

New way to learn PHP

PHP 5 $$Variable

What is the difference between $variable and $$variable?

$variable

PHP $variable (Single dollar variable) is a simple variable that store the some value like below shown.

<?php

$a=15 ."<br/>";
$b="www.ptutorial.com";
echo $a;
echo $b;

?>

Output

15
www.ptutorial.com

$a and $b both are simple variables where $a is an integer variable and $b is a string variable you can easily access the value of this variable as shown in variable page.

PHP $$variable

PHP $$variable (double dollar variable) is a reference variable that store the value of the variable whose name is $variable $$branch is a reference of $branch, where $branch is a simple variable that can hold anything like integer, String, float etc. But $$branch is a reference variable that store the variable $branch.

<?php

$branch = "CSE";
$CSE="me ";
echo $branch."<br/>";
echo $$branch."<br/>"; 
echo $CSE."<br/>";

?>

Output

CSE
me
me

In this example, $branch is a simple variable $$branch is a reference variable that point the value of $CSE variable value.

<?php

$branch = "CSE";
${$branch}="Computer Science & engineering";
echo $branch."<br/>";
echo $$branch."<br/>";
echo $CSE;

?>

Output

CSE
Computer Science & engineering
Computer Science & engineering

In this case, $branch is a variable that store the CSE and $CSE is another variable that hold the Computer Science & engineering.

When program executes $branch replace with the $CSE. If you print the $branch then the value will be CSE and when you print $$branch that means $CSE ($branch is replaced by the CSE) and the value will be Computer Science & engineering.

And you can also access the value of CSE using simple echo statement.


QUOTES OF THE DAY

Tomorrow life is too late, live today.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER