php php中的$a和$$a有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11504335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:40:52  来源:igfitidea点击:

What is the difference between $a and $$a in php?

php

提问by Jalpesh Patel

Possible Duplicate:
What does $$ mean in PHP?

可能的重复:
$$ 在 PHP 中是什么意思?

I am new to PHP and I don't know what the difference between $aand $$ais.

我是 PHP 新手,我不知道$a和之间有什么区别$$a

回答by Andreas Linden

$arepresents a variable

$a代表一个变量

$$arepresents a variable with the content of $a

$$a表示一个变量,内容为 $a

example:

例子:

$test = "hello world";
$a = "test";
echo $$a;

output will be hello world

输出将是 hello world

回答by Quentin

If $a = 'b'then $$ais $b.

如果$a = 'b'然后$$a$b

This is a variable variable. They are evil. Use arrays instead (which do the same thing, but more maintainably and with the ability to use array functions on them).

这是一个可变变量。他们是邪恶的。使用数组代替(它们做同样的事情,但更易于维护并且能够在它们上使用数组函数)。

回答by qaisjp

$variable is a normal variable $$variable takes the value of a variable and treats that as the name of a variable

$variable 是一个普通变量 $$variable 获取变量的值并将其视为变量的名称

eg:

例如:

$var = 'welcome';
echo $var //prints welcome

$$var = 'to stackoverflow';

echo "$var ${$var}"; //prints welcome to stackoverflow
echo "$var $welcome"; //prints welcome to stackoverflow

回答by Naren Karthik

Double dollar is a powerful way to programmatically create variables and assign values them.

Double Dollar 是一种以编程方式创建变量并为其赋值的强大方法。

E.g:

例如:

<?php

$a = “amount”;
$$a =1000;
echo $amount; //echo's 1000 on screen

?>

In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.

在上面的示例中,您可以看到变量 $a 存储值“amount”。当您使用双美元符号 ($$) 时,您间接引用了 $a 的值,即金额。

So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.

所以,像这样 $$a = 1000; 变量 $amount 被创建,我将值 1000 分配给 $amount。通过这种方式,您可以以编程方式创建变量并为其赋值。

回答by Alnitak

$ais the contents of the variable a, $$ais the contents of the variable namedin $a.

$a是变量的内容a$$a是变量的内容命名$a

Don't use this syntax in your own code.

不要在您自己的代码中使用此语法。

回答by poncha

$$ais a variable which name is in $a

$$a是名称所在的变量 $a

Assuming $a = "foo";, $$awill be same as $foo

假设$a = "foo";,$$a将与$foo

回答by WolvDev

In PHP each variable starts with an $.

在 PHP 中,每个变量都以 $ 开头。

So for example you have the variable $a = 'var';

例如,您有变量$a = 'var';

So $$a == $var

所以 $$a == $var

This new variable will have the "content" of the other variable as name.

这个新变量将以另一个变量的“内容”作为名称。