-1

我正在尝试学习 php,我在 foreach 循环中看到了这个,这是什么意思?我理解 &$var 它是对对象内存地址的直接引用。但是 $$var 是什么意思呢?究竟是什么?

这就是例子。

    foreach($this->vars as $key => $value)
    {
        $$key = $value;
        echo "$$Key: " . $$key;
        echo "Key: " . $key;
        echo "<br/>";
        echo "Value: " . $value;
    }
4

3 回答 3

5

您正在查看一个变量 variable。例如

// original variable named 'foo'
$foo = "bar";

// reference $foo dynamically by evaluating $x
$x = "foo";
echo $$x; // "bar";
echo ${$x}; // "bar" as well but the {} allows you to perform concatenation

// different version of {} to show a more "complex" operation
$y = "fo";
$z = "o";
echo ${$y . $z}; // "bar" also ("fo" . "o" = "foo")

要显示一个更接近您的问题的示例:

$foo = "foo";
$bar = "bar";
$baz = "baz";

$ary = array('foo' => 'FOO','bar' => 'BAR','baz' => 'BAZ');
foreach ($ary as $key => $value){
  $$key = $value;
}

// end result is:
// $foo = "FOO";
// $bar = "BAR";
// $baz = "BAZ";
于 2013-10-07T15:51:21.597 回答
2

它是一个名为 的变量$key。例如,如果$k='somevar',那么$$k = $somevar

于 2013-10-07T15:53:02.287 回答
1

变量变量。var 名称是包含在$var.

如果$key = 'test'then$$key将被评估为一个名为$test.

此外,实际用途很少。大多数情况下,数组会更好。

于 2013-10-07T15:51:39.050 回答