2010-04-26 36 views
78

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

global $$link; 

是什么$$意思?

+8

*(参考)* http://de3.php.net/manual/en/language.variables.variable.php – Gordon 2010-04-26 17:57:02

+27

它显示了你将获得多少$$$维护系统; ) – RCIX 2010-05-01 04:05:58

+1

*(相关)* [这是什么符号在PHP中的意思](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon 2011-11-17 09:25:03

回答

125

语法如$$variable称为Variable Variable


例如,如果你考虑的这部分程序:

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

您将得到以下输出:

test 


这里:

  • $real_variable包含TE ST
  • $name包含您的变量名:'real_variable'
  • $$name意味着 “变量全髋关节置换已包含在$name它的名字”
    • 这是$real_variable
    • 并拥有价值'test'



编辑后@的Jhonny的评论:

做一个$$$
好了,知道最好的办法就是尽量;-)

那么,让我们试试这个代码部分:

$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 />'; 

和这里的输出我得到:

name 
real_variable 
test 

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

+7

虽然我同意它可能是有用的,大多数情况下最好使用数组。 – 2010-04-26 18:31:26

+0

所以和做$ {$ value}一样吗? – Chaim 2010-04-26 20:39:21

+2

@Chaim:是的,它是;;除了在某些情况下需要'{'和'}'*(手册中有一个例子)* – 2010-04-26 20:41:37

11

这是一个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" 
?> 
1

它评估一个变量的内容作为另一个的名字。基本上它给你的变量名称存储在$link

19

内部$将一个变量解析为一个字符串,而外部的一个通过该字符串解析变量。

因此,考虑这个例子

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

变量:

$$outer 

将等于字符串 “foo”

5

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

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

(也称为variable variable

3

我不希望别人后重复,但存在使用$$ :)

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

因此,与头使用它的风险。 :)