3

Possible Duplicate:
what does $$ mean in PHP?

I recently needed to make a change on a application and came across this $pageObject->createPageContent($$templateName);

The method looked like this

function createPageContent($page_content_html) {
        $this->page_content = $page_content_html;
    }

My question is when I removed the one $ sign infront of the variable I got a different result as with the double $$. Why is there one $ sign extra? What's the purpose for this?

Community
  • 1
  • 1
Elitmiar
  • 35,072
  • 73
  • 180
  • 229

4 Answers4

7

$$ signifies a variable variable in PHP.

It's an easy way to reference an already existing variable by a string.

Here's an example:

$someVar = 'something';

$varname = 'someVar';

echo $$varname; //something

So, in your example, $templateName actually references the name of an already existing variable, so when you prepend it with another $, PHP gets the value out of that variable. This is a very powerful language feature IMHO.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
4

$$ represents a variable variable. The result of $templateName is used as the variable name you wish to reference. For further clarity, it can also be written as

${$templateName}

For example,

$templateName = "hello";
$hello = "world";

echo $$templateName;
//-> "world"

http://php.net/manual/en/language.variables.variable.php

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • I hate it when people upvote one answer over the other when they are actually very very similar. You should be getting equal upvotes. – Jacob Relkin Nov 05 '10 at 11:59
  • not to mention that I was already over the rep cap before answering. – Jacob Relkin Nov 05 '10 at 12:02
  • @Jacob: not to worry - if I'm running out of votes I usually go with faster finger first (when the answers are more or less the same), which was you by 25 seconds :-) – Andy E Nov 05 '10 at 12:07
1

When you're using two $$ it means the name of the variable is actually a veriable.

$animal = "cow";

$cow = "moo";

echo $$animal;

//Prints 'moo'
Bjorn
  • 69,215
  • 39
  • 136
  • 164
0

This way you are able to address variables dynamically.

There is a perfect description to be found at php.net.

Thariama
  • 50,002
  • 13
  • 138
  • 166