126

Example is a variable declaration within a function:

global $$link;

What does $$ mean?

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
chicane
  • 1,831
  • 3
  • 18
  • 15

7 Answers7

197

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
  • 374,560
  • 73
  • 631
  • 650
24

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
  • 34,878
  • 31
  • 108
  • 151
  • Even though the accepted anwser is a lot broader (including test cases). This explanation made it clearer for me – MariusJP Sep 11 '18 at 14:30
13

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
  • 84,001
  • 14
  • 111
  • 112
7

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
  • 705,106
  • 160
  • 1,004
  • 1,072
4

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
  • 136,835
  • 55
  • 236
  • 297
1

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

Valery Viktorovsky
  • 5,832
  • 3
  • 32
  • 42
Zach
  • 6,942
  • 3
  • 19
  • 26
1

this worked for me (enclose in square brackets):

$aInputsAlias = [
        'convocatoria'   => 'even_id',
        'plan'           => 'acev_id',
        'gasto_elegible' => 'nivel1',
        'rubro'          => 'nivel2',
        'grupo'          => 'nivel3',
    ];

    /* Manejo de los filtros */

    foreach(array_keys($aInputsAlias) as $field)
    {
        $key = $aInputsAlias[$field];

        ${$aInputsAlias[$field]} = $this->request->query($field) ? $this->request->query($field) : NULL;
    }