What does $$ (dollar dollar or double dollar) mean in PHP?

Example is a variable declaration within a function:

global $$link;

What does $$ mean?


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 $$$ ;-)


    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"


    这是一个变量的变量。

    <?php
    $a = 'hello';
    $$a = 'world'; // now makes $hello a variable that holds 'world'
    echo "$a ${$a}"; // "hello world"
    echo "$a $hello"; // "hello world"
    ?>
    
    链接地址: http://www.djcxy.com/p/1840.html

    上一篇: 什么是PHP中的“$$”

    下一篇: PHP(美元美元或双倍美元)在PHP中意味着什么?