-2

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?

RavatSinh Sisodiya
  • 1,596
  • 1
  • 20
  • 42
user3085676
  • 112
  • 1
  • 5

2 Answers2

7

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.
user3085576
  • 167
  • 1
  • 1
  • 5
1

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

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134