Why are there two $$ signs infront of variable?
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?
$$
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.
$$
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
当你使用两个$$时,这意味着变量的名字实际上是可以理解的。
$animal = "cow";
$cow = "moo";
echo $$animal;
//Prints 'moo'
链接地址: http://www.djcxy.com/p/59094.html
上一篇: PHP中的$$是什么?
下一篇: 为什么在变量的前面有两个$$符号?