11

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.

Community
  • 1
  • 1
Jalpesh Patel
  • 3,064
  • 10
  • 41
  • 66
  • what is the problem in my question? why people making -1?? – Jalpesh Patel Jul 16 '12 at 12:37
  • Because it's a duplicate and this question has been answered very well in an other thread. Make a search before asking net time. – j0k Jul 16 '12 at 12:40
  • @j0k: in all fairness, $$ is a difficult term to search for. However, the criteria for a down vote should be *"does not show research effort; is unclear or not useful"*. I down voted because the question shows no research effort. – Andy E Jul 16 '12 at 12:41
  • i am search it on the google same like i am asked but i am not get it properly then after i am asking it here...and sorry for the my question duplication – Jalpesh Patel Jul 16 '12 at 12:43

7 Answers7

27

$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

Andreas Linden
  • 11,975
  • 7
  • 46
  • 65
19

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).

Lal
  • 14,505
  • 4
  • 39
  • 63
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • @DhruvThakkar — There are seven explanations here, and a further six beyond the duplicate link, already. If you don't understand any of them then I don't see how I can explain it in a way that you do understand. – Quentin Feb 28 '19 at 09:54
6

$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
qaisjp
  • 621
  • 7
  • 29
5

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.

Naren Karthik
  • 349
  • 2
  • 9
4

$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.

Alnitak
  • 313,276
  • 69
  • 379
  • 466
3

$$a is a variable which name is in $a

Assuming $a = "foo";, $$a will be same as $foo

poncha
  • 7,198
  • 1
  • 31
  • 36
2

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.

WolvDev
  • 3,094
  • 1
  • 15
  • 30