I have doubt regrading '$' usage in PHP.
In php is their any difference between between $b
and $$b
?
What will be the output then for $b
and $$b
?

- 1,596
- 1
- 20
- 42

- 112
- 1
- 5
-
1Read here http://php.net/manual/en/language.variables.variable.php – Shankar Narayana Damodaran Dec 10 '13 at 06:34
-
4`user3085676` asks , `user3085576` answers O.o – Shankar Narayana Damodaran Dec 10 '13 at 06:36
-
1user3085676 vs user3085676 – Krish R Dec 10 '13 at 06:37
-
This is a fairly valid question, but had you typed it in to Google, you would have gotten your answer without going through the hassle of being massively downvoted on SO :| – brandonscript Dec 10 '13 at 07:04
-
First i am sorry that i answered my own question.Because i got an answer from google itself. As i wanted to share what i got, i answered and shared with people. People come here to get a complex solution explained in a simple way.Google has everything in it i agree.. If all looks in google then why we would use stackoverflow? – user3085576 Dec 11 '13 at 04:13
2 Answers
The only difference between $message and $$b is that, $b is a normal variable and $$bis variable to variable. The difference in functioning is shown below:
When declaring a variable in PHP the variable gets declared like this
$b//which is simply a variable
To store the data to assign the value to it we write like
$b= “ride”; //assigned the string to the variable
echo $b; // it will print the value
Whereas if you want to display the variable to variable then you use
$var="Hello";
$b="var";
echo $b; //print var
echo $$b; //print Hello.

- 167
- 1
- 1
- 5
From the PHP manual:
A normal variable is set with a statement such as:
$a = 'hello';
A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello
, can be used as the name of a variable by using two dollar signs. i.e.
$$a = 'world';
At this point two variables have been defined and stored in the PHP symbol tree: $a
with contents "hello" and $hello
with contents "world".

- 20,529
- 24
- 107
- 134