Possible Duplicate:
What does $$ mean in PHP?
I am new to PHP and I don't know what the difference between $a
and $$a
is.
Possible Duplicate:
What does $$ mean in PHP?
I am new to PHP and I don't know what the difference between $a
and $$a
is.
$a
represents a variable
$$a
represents a variable with the content of $a
example:
$test = "hello world";
$a = "test";
echo $$a;
output will be hello world
If $a = 'b'
then $$a
is $b
.
This is a variable variable. They are evil. Use arrays instead (which do the same thing, but more maintainably and with the ability to use array functions on them).
$variable is a normal variable $$variable takes the value of a variable and treats that as the name of a variable
eg:
$var = 'welcome';
echo $var //prints welcome
$$var = 'to stackoverflow';
echo "$var ${$var}"; //prints welcome to stackoverflow
echo "$var $welcome"; //prints welcome to stackoverflow
Double dollar is a powerful way to programmatically create variables and assign values them.
E.g:
<?php
$a = “amount”;
$$a =1000;
echo $amount; //echo’s 1000 on screen
?>
In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.
So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.
$a
is the contents of the variable a
, $$a
is the contents of the variable named in $a
.
Don't use this syntax in your own code.
$$a
is a variable which name is in $a
Assuming $a = "foo";
, $$a
will be same as $foo
In PHP each variable starts with an $.
So for example you have the variable $a = 'var'
;
So $$a == $var
This new variable will have the "content" of the other variable as name.