-1

I am trying to learn php, and I saw this in a foreach loop what does it mean? I understand &$var which its a direct reference to the memory address of the object. But what does $$var means? what is it exactly?

This is the example.

    foreach($this->vars as $key => $value)
    {
        $$key = $value;
        echo "$$Key: " . $$key;
        echo "Key: " . $key;
        echo "<br/>";
        echo "Value: " . $value;
    }
S.H.
  • 2,833
  • 3
  • 26
  • 28
  • 6
    It is a [variable variable](http://php.net/manual/en/language.variables.variable.php). – Lix Oct 07 '13 at 15:50
  • There are tutorials that cover this topic, this shouldn't be asked on SO, especially since the answer is available online from plenty of sources. – Mark Oct 07 '13 at 15:51
  • 4
    @downvoters Lay off the down votes, use your close votes, this is what they're for. – user229044 Oct 07 '13 at 15:52
  • 2
    Use [SymbolHound](http://symbolhound.com/?q=what+is+%24%24var+php) for such search queries. – Amal Murali Oct 07 '13 at 15:54
  • Nothing to do with references. – AbraCadaver Oct 07 '13 at 15:54
  • @AbraCadaver: Might help if you read that question :) – Amal Murali Oct 07 '13 at 15:56
  • Ahh I get it thanks... although I couldn't FIND it in the internet, because I didnt know how to put the question in GOOGLE. Like "WHAT IS $$var in php"... didn't give me anything as actual answer, so thats why I aksed in SO. The wierd thing about that code in a template is why would I have variable variables? what would they be used for? Well thanks anyways. – S.H. Oct 07 '13 at 15:57
  • @Amal Murali: I did. He said "I understand &$var. But what does $$var means?" Not a reference that's for sure. :) – AbraCadaver Oct 07 '13 at 21:26
  • @AbraCadaver: *Please* read [this](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) question. It's not about PHP reference. Reference as in mention/citiation. – Amal Murali Oct 07 '13 at 21:30

3 Answers3

5

You're looking at a variable variable. e.g.

// 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")

To show an example more closely matching your question:

$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";
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
2

It's a variable with the name $key. For example, If $k='somevar', then $$k = $somevar.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Sam Braslavskiy
  • 1,268
  • 10
  • 19
1

Variable variable. The var name is what is contained in $var.

If $key = 'test' then $$key will be evaluate to a var named $test.

Also, there are very few practical uses. Most often arrays would be better.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87