开发者

i got this PHP question from my friend about variable dollar sign [duplicate]

开发者 https://www.devze.com 2023-01-18 03:08 出处:网络
This question already has answers here: Closed 12 years ago. Possible Du开发者_Python百科plicate: what does $$ mean in PHP?
This question already has answers here: Closed 12 years ago.

Possible Du开发者_Python百科plicate:

what does $$ mean in PHP?

what is the different between $thisvariable and $$thisvariable. as you notice, the first variable has one dollar sign while the second got two dollar signs.


$variable is a variable and $$variable is a variable variables,

$my_name = "anthony"; // variable $my_name 
echo $my_name; // outputs "anthony"

$a_var = "my_name"; // assigning literal to variable $a_var
echo $$a_var; // outputs "anthony"

It may be a bit confusing, so let's break down that echo call,

$($a_var)  
   -> $(my_name) 
       -> $my_name = "anthony"

Please note that the above may not be what happens behind the scenes of the PHP interpreter, but it serves strictly as an illustration.

Hope this helps.


$thisvariable is a variable named $thisvariable:

$thisvariable = 'Hello';
print $thisvariable; // prints Hello

$$thisvariable is a variable variable:

$thisvariable = 'Hello';
$Hello = 'Greetings';
print $$thisvariable; // prints Greetings ($$thisvariable is equivalent to $Hello)

For the most part you should avoid using variable variables. It makes the code harder to understand and debug. When I see it it's a red flag that there's some bad design.

0

精彩评论

暂无评论...
验证码 换一张
取 消