首頁 > php教程 > php手册 > PHP的学习--可变变量,--可变变量

PHP的学习--可变变量,--可变变量

WBOY
發布: 2016-06-13 08:50:19
原創
1193 人瀏覽過

PHP的学习--可变变量,--可变变量

可变变量

有时候使用可变变量名是很方便的。就是说,一个变量的变量名可以动态的设置和使用。一个普通的变量通过声明来设置,例如:

<?<span>php
</span><span>$a</span> = 'hello'<span>;
</span>?>
登入後複製

一个可变变量获取了一个普通变量的值作为这个可变变量的变量名。在上面的例子中 hello 使用了两个美元符号($)以后,就可以作为一个可变变量的变量了。例如:

<?<span>php
$</span><span>$a</span> = 'world'<span>;
</span>?>
登入後複製

这时,两个变量都被定义了:$a 的内容是“hello”并且 $hello 的内容是“world”。因此,以下语句:

<?<span>php
</span><span>echo</span> "<span>$a</span> ${<span>$a</span>}"<span>;
</span>?>
登入後複製

与以下语句输出完全相同的结果:

<?<span>php
</span><span>echo</span> "<span>$a</span> <span>$hello</span>"<span>;
</span>?>
登入後複製

它们都会输出:hello world。

要将可变变量用于数组,必须解决一个模棱两可的问题。这就是当写下 $$a[1] 时,解析器需要知道是想要 $a[1] 作为一个变量呢,还是想要$$a 作为一个变量并取出该变量中索引为 [1] 的值。解决此问题的语法是,对第一种情况用 ${$a[1]},对第二种情况用 ${$a}[1]

类的属性也可以通过可变属性名来访问。可变属性名将在该调用所处的范围内被解析。例如,对于 $foo->$bar 表达式,则会在本地范围来解析$bar 并且其值将被用于 $foo 的属性名。对于 $bar 是数组单元时也是一样。

也可使用花括号来给属性名清晰定界。最有用是在属性位于数组中,或者属性名包含有多个部分或者属性名包含有非法字符时(例如来自json_decode() 或 SimpleXML)。

Example #1 可变属性示例

<?<span>php
</span><span>class</span><span> foo {
    </span><span>var</span> <span>$bar</span> = 'I am bar.'<span>;
    </span><span>var</span> <span>$arr</span> = <span>array</span>('I am A.', 'I am B.', 'I am C.'<span>);
    </span><span>var</span> <span>$r</span>   = 'I am r.'<span>;
}

</span><span>$foo</span> = <span>new</span><span> foo();
</span><span>$bar</span> = 'bar'<span>;
</span><span>$baz</span> = <span>array</span>('foo', 'bar', 'baz', 'quux'<span>);
</span><span>echo</span> <span>$foo</span>-><span>$bar</span> . "\n"<span>;
</span><span>echo</span> <span>$foo</span>-><span>$baz</span>[1] . "\n"<span>;

</span><span>$start</span> = 'b'<span>;
</span><span>$end</span>   = 'ar'<span>;
</span><span>echo</span> <span>$foo</span>->{<span>$start</span> . <span>$end</span>} . "\n"<span>;

</span><span>$arr</span> = 'arr'<span>;
</span><span>echo</span> <span>$foo</span>-><span>$arr</span>[1] . "\n"<span>;
</span><span>echo</span> <span>$foo</span>->{<span>$arr</span>}[1] . "\n"<span>;

</span>?>
登入後複製

以上例程会输出:

I am bar.<span>
I am bar</span>.<span>
I am bar</span>.<span>
I am r</span>.<span>
I am B</span>.
登入後複製

Example #2 可变属性示例

<?<span>php

  </span><span>//</span><span>You can even add more Dollar Signs</span>

  <span>$Bar</span> = "a"<span>;
  </span><span>$Foo</span> = "Bar"<span>;
  </span><span>$World</span> = "Foo"<span>;
  </span><span>$Hello</span> = "World"<span>;
  </span><span>$a</span> = "Hello"<span>;

  </span><span>$a</span>; <span>//</span><span>Returns Hello</span>
  $<span>$a</span>; <span>//</span><span>Returns World</span>
  $$<span>$a</span>; <span>//</span><span>Returns Foo</span>
  $$$<span>$a</span>; <span>//</span><span>Returns Bar</span>
  $$$$<span>$a</span>; <span>//</span><span>Returns a</span>
<span>
  $$$$$</span><span>$a</span>; <span>//</span><span>Returns Hello</span>
  $$$$$$<span>$a</span>; <span>//</span><span>Returns World

  //... and so on ...//</span>

?>
登入後複製

Example #3 可变属性示例

<?<span>php
</span><span>//</span><span> Given these variables ...</span>
<span>$nameTypes</span>    = <span>array</span>("first", "last", "company"<span>);
</span><span>$name_first</span>   = "John"<span>;
</span><span>$name_last</span>    = "Doe"<span>;
</span><span>$name_company</span> = "PHP.net"<span>;

</span><span>//</span><span> Then this loop is ...</span>
<span>foreach</span>(<span>$nameTypes</span> <span>as</span> <span>$type</span><span>)
  </span><span>print</span> ${"name_<span>$type</span>"} . "\n"<span>;

</span><span>//</span><span> ... equivalent to this print statement.</span>
<span>print</span> "<span>$name_first</span>\n<span>$name_last</span>\n<span>$name_company</span>\n"<span>;
</span>?>
登入後複製

以上例程会输出:

<span>John
Doe
PHP</span>.<span>net
John
Doe
PHP</span>.net
登入後複製

 

摘自:http://php.net/manual/zh/functions.variable-functions.php

 

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板