tags:

views:

271

answers:

7

example a variable declaration within a function:

what is $$ mean?

global $$link;

thanks

+2  A: 

It's a variable's variable.

<?php
$a = 'hello';
$$a = 'world'; // now makes $hello a variable that holds 'world'
echo "$a ${$a}"; // "hello world"
echo "$a $hello"; // "hello world"
?>
Anthony Forloney
+20  A: 

A syntax such as $$variable is called Variable Variable.


For example, if you consider this portion of code :

$real_variable = 'test';
$name = 'real_variable';
echo $$name;

You will get the following output :

test


Here :

  • $real_variable contains test
  • $name contains the name of your variable : 'real_variable'
  • $$name mean "the variable thas has its name contained in $name"
    • Which is $real_variable
    • And has the value 'test'



EDIT after @Jhonny's comment :

Doing a $$$ ?
Well, the best way to know is to try ;-)

So, let's try this portion of code :

$real_variable = 'test';
$name = 'real_variable';
$name_of_name = 'name';

echo $name_of_name . '<br />';
echo $$name_of_name . '<br />';
echo $$$name_of_name . '<br />';

And here's the output I get :

name
real_variable
test

So, I would say that, yes, you can do $$$ ;-)

Pascal MARTIN
and can you do a $$$ ?
Jhonny D. Cano -Leftware-
What a dumb language!
Hamish Grubijan
Actually Hamish, this trick is incredibly useful in the right circumstances.
keithjgrant
@Jhonny : it seems you can ;-) *(I've edited my answer to provide an example doing just that)*
Pascal MARTIN
While I agree that it can be useful, most of the times it's better to use arrays anyway.
Lo'oris
@Hamish PHP sucks but it doesn't matter: http://www.codinghorror.com/blog/2008/05/php-sucks-but-it-doesnt-matter.html
Jhonny D. Cano -Leftware-
so is this the same thing as doing ${$value}?
Chaim Chaikin
@Chaim : yes it is ;; except that, in some cases, the `{` and `}` are required *(there's an example in the manual about that)*
Pascal MARTIN
+1  A: 

It evaluates the contents of one variable as the name of another. Basically it gives you the variable whose name is stored in $link.

Zach
+6  A: 

The inner $ resolves the a variable to a string, and the outer one resolves a variable by that string.

So, consider this example

$inner = "foo";
$outer = "inner";

The variable:

$$outer

would equal the string "foo"

Rich
+4  A: 

It creates a dynamic variable name. E.g.

$link = 'foo';
$$link = 'bar';    // -> $foo = 'bar'
echo $foo;
// prints 'bar'

(also known as variable variable)

Felix Kling
A: 

I do not want to repeat after others but there is a risk using $$ :)

$a  = '1';
$$a =  2; // $1 = 2 :)

So use it with head. :)

hsz
Better-yet: don't use it at all.
notJim
A: 

global $$link; does mean terrible application design.
One who wrote this have no idea of code support

Col. Shrapnel