Php 简明教程

PHP - $ and $$ Variables

我们知道 PHP 使用“$”符号作为变量名前缀的惯例。PHP 还可以通过在名称之前添加两个美元符号 ($$) 来声明动态变量。变量变量(或动态变量)可以动态设置和使用。

We know that PHP uses the convention of prefixing the variable names by the "$" symbol. PHP also has the provision of declaring dynamic variables by prefixing two dollar symbols ($$) to the name. A variable variable (or a dynamic variable) can be set and used dynamically.

普通变量的声明如下 −

The declaration of a normal variable is like this −

$a = 'good';

动态变量取正常变量的值并将其作为变量的名称。在上面的示例中,“good”可以使用两个美元符号“$$”作为变量名 −

A dynamic variable takes the value of a normal variable and treats that as the name of the variable. In the above example, "good" can be used as the name of a variable by using two dollar signs "$$" −

$$a = 'morning';

我们现在有两个变量:“$a”内容为“good”和“$$a”内容为“morning”。因此,以下 echo 语句将产生相同输出 −

We now have two variables: "$a" with contents "good" and "$$a" with contents "morning". As a result, the following echo statements will produce the same output −

echo "$a {$$a}";
echo "$a $good";

两者都产生相同的输出 −

Both produce the same output −

good morning

Example 1

看看下面的 example

Take a look at this following example

<?php
   $a = 'good';
   $$a = 'morning';

   echo "$a {$$a}\n";
   echo "$a $good";
?>

它将生成以下 output

It will produce the following output

good morning
good morning

Example 2

我们来看另一个示例 −

Let’s take a look at another example −

<?php
   $x = "foo";
   $$x = "bar";
   echo "Value of x = " .$x . "\n";
   echo 'Value of $$x = ' . $$x . "\n";
   echo 'Value of foo = ' . $foo;
?>

在这里,你会得到以下 output

Here, you will get the following output

Value of x = foo
Value of $$x = bar
Value of foo = bar

Using Multiple "$" Symbols

请注意,美元符号“$”的使用并不限于两个。可以添加任意数量的美元符号作为前缀。

Note that the use of "$" symbol is not restricted to two. Any number of dollar symbols can be prefixed.

假设有一个变量“$x”其值为“a”。接下来,我们定义 x='as',那么“x”和“$a”将具有相同的值。同样,语句 $$$x='and' 实际上声明了一个“$as”变量,其值为’and'。

Suppose there is a variable "$x" with "a" as its value. Next, we define x='as', then "x" as well as "$a" will have the same value. Similarly, the statement $$$x='and' effectively declares a "$as" variable whose value is 'and'.

Example

这是一个完整的示例,展示了多个“$”符号的使用。

Here is a complete example that shows the use of multiple "$" symbols.

<?php
   $php = "a";
   $lang = "php";
   $World = "lang";
   $Hello = "World";
   $a = "Hello";
   echo '$a= ' . $a;
   echo "\n";
   echo '$$a= ' . $$a;
   echo "\n";
   echo '$$$a= ' . $$$a;
   echo "\n";
   echo '$$$$a= ' . $$$$a;
   echo "\n";
   echo '$$$$$a= ' . $$$$$a;
?>

当您运行此代码时,它将生成以下 output

When you run this code, it will produce the following output

$a= Hello
$$a= World
$$$a= lang
$$$$a= php
$$$$$a= a

Using Dynamic Variables with Arrays

将动态变量与数组一起使用可能会导致某些歧义的情况。对于数组“a”,如果你写 a[1],那么解析器需要知道你是否正在引用“$a[1]”作为变量,或者你希望“a”作为变量,然后从该变量中获取 [1] 索引。

Using dynamic variables with arrays may lead to certain ambiguous situations. With an array "a", if you write a[1], then the parser needs to know if you are refering to "$a[1]" as a variable or if you want "a" as the variable and then the [1] index from that variable.

若要解决此歧义,请对第一种情况使用 ${$a[1]},对第二种情况使用 ${$a}[1]。

To resolve this ambiguity, use ${$a[1]} for the first case and ${$a}[1] for the second.

Example

请看以下示例:

Take a look at the following example −

<?php
   $vars = array("hw", "os", "lang");
   $var_hw="Intel";
   $var_lang="PHP";
   $var_os="Linux";

   foreach ($vars as $var)
      echo ${"var_$var"} . "\n";

   print "$var_hw\n$var_os\n$var_lang";
?>

它将生成以下 output

It will produce the following output

Intel
Linux
PHP
Intel
Linux
PHP

需要注意的是,这个技术不能在函数或类方法中与 PHP 的超全局数组(PHP 中的几个预定义变量是“超全局”,这意味着它们在整个脚本中的所有作用域中都可用)一起使用。变量“$this”是 PHP 中的一个特殊变量,不能动态引用。

It may be noted that this technique cannot be used with PHP’s Superglobal arrays (Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script) within functions or class methods. The variable "$this" is a special variable in PHP and it cannot be referenced dynamically.