$$(美元或双美元)在 PHP 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2715654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:28:10  来源:igfitidea点击:

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

php

提问by chicane

Example is a variable declaration within a function:

示例是函数内的变量声明:

global $$link;

What does $$mean?

什么$$意思?

回答by Pascal MARTIN

A syntax such as $$variableis called Variable Variable.

一种$$variable称为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_variablecontains test
  • $namecontains the name of your variable : 'real_variable'
  • $$namemean "the variable thas has its name contained in $name"
    • Which is $real_variable
    • And has the value 'test'
  • $real_variable包含测试
  • $name包含变量的名称: 'real_variable'
  • $$name意思是“变量名包含在$name”中
    • 这是 $real_variable
    • 并且具有价值 'test'





EDIT after @Jhonny's comment :

在@Jhonny 的评论后编辑:

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

所以,我会说,是的,你可以做到$$$;-)

回答by Rich

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

内部 $ 将 a 变量解析为字符串,外部 $ 将变量解析为该字符串。

So, consider this example

所以,考虑这个例子

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

The variable:

变量:

$$outer

would equal the string "foo"

将等于字符串“foo”

回答by Anthony Forloney

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

回答by Felix Kling

It creates a dynamic variable name. E.g.

它创建一个动态变量名。例如

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

(also known as variable variable)

(也称为可变变量

回答by hsz

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

我不想在其他人之后重复,但使用有风险$$:)

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

So use it with head. :)

所以用头。:)

回答by Diego Santa Cruz Mendezú

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;
    }

回答by Zach

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

它将一个变量的内容评估为另一个变量的名称。基本上它为您提供名称存储在$link.