Maison >développement back-end >tutoriel php >PHP的学习-可变变量
PHP的学习--可变变量
有时候使用可变变量名是很方便的。就是说,一个变量的变量名可以动态的设置和使用。一个普通的变量通过声明来设置,例如:
<span style="color: #000000;">php</span><span style="color: #800080;">$a</span> = 'hello'<span style="color: #000000;">;</span>?>
一个可变变量获取了一个普通变量的值作为这个可变变量的变量名。在上面的例子中 hello 使用了两个美元符号($)以后,就可以作为一个可变变量的变量了。例如:
<span style="color: #000000;">php$</span><span style="color: #800080;">$a</span> = 'world'<span style="color: #000000;">;</span>?>
这时,两个变量都被定义了:$a 的内容是“hello”并且 $hello 的内容是“world”。因此,以下语句:
<span style="color: #000000;">php</span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$a</span> ${<span style="color: #800080;">$a</span>}"<span style="color: #000000;">;</span>?>
与以下语句输出完全相同的结果:
<span style="color: #000000;">php</span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$a</span> <span style="color: #800080;">$hello</span>"<span style="color: #000000;">;</span>?>
它们都会输出:hello world。
要将可变变量用于数组,必须解决一个模棱两可的问题。这就是当写下 $$a[1] 时,解析器需要知道是想要 $a[1] 作为一个变量呢,还是想要$$a 作为一个变量并取出该变量中索引为 [1] 的值。解决此问题的语法是,对第一种情况用 ${$a[1]},对第二种情况用 ${$a}[1]。
类的属性也可以通过可变属性名来访问。可变属性名将在该调用所处的范围内被解析。例如,对于 $foo->$bar 表达式,则会在本地范围来解析$bar 并且其值将被用于 $foo 的属性名。对于 $bar 是数组单元时也是一样。
也可使用花括号来给属性名清晰定界。最有用是在属性位于数组中,或者属性名包含有多个部分或者属性名包含有非法字符时(例如来自json_decode() 或 SimpleXML)。
Example #1 可变属性示例
<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> foo { </span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$bar</span> = 'I am bar.'<span style="color: #000000;">; </span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span>('I am A.', 'I am B.', 'I am C.'<span style="color: #000000;">); </span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$r</span> = 'I am r.'<span style="color: #000000;">;}</span><span style="color: #800080;">$foo</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> foo();</span><span style="color: #800080;">$bar</span> = 'bar'<span style="color: #000000;">;</span><span style="color: #800080;">$baz</span> = <span style="color: #0000ff;">array</span>('foo', 'bar', 'baz', 'quux'<span style="color: #000000;">);</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$foo</span>-><span style="color: #800080;">$bar</span> . "\n"<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$foo</span>-><span style="color: #800080;">$baz</span>[1] . "\n"<span style="color: #000000;">;</span><span style="color: #800080;">$start</span> = 'b'<span style="color: #000000;">;</span><span style="color: #800080;">$end</span> = 'ar'<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$foo</span>->{<span style="color: #800080;">$start</span> . <span style="color: #800080;">$end</span>} . "\n"<span style="color: #000000;">;</span><span style="color: #800080;">$arr</span> = 'arr'<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$foo</span>-><span style="color: #800080;">$arr</span>[1] . "\n"<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$foo</span>->{<span style="color: #800080;">$arr</span>}[1] . "\n"<span style="color: #000000;">;</span>?>
以上例程会输出:
I am bar.<span style="color: #000000;">I am bar</span>.<span style="color: #000000;">I am bar</span>.<span style="color: #000000;">I am r</span>.<span style="color: #000000;">I am B</span>.
Example #2 可变属性示例
<span style="color: #000000;">php </span><span style="color: #008000;">//</span><span style="color: #008000;">You can even add more Dollar Signs</span> <span style="color: #800080;">$Bar</span> = "a"<span style="color: #000000;">; </span><span style="color: #800080;">$Foo</span> = "Bar"<span style="color: #000000;">; </span><span style="color: #800080;">$World</span> = "Foo"<span style="color: #000000;">; </span><span style="color: #800080;">$Hello</span> = "World"<span style="color: #000000;">; </span><span style="color: #800080;">$a</span> = "Hello"<span style="color: #000000;">; </span><span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">Returns Hello</span> $<span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">Returns World</span> $$<span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">Returns Foo</span> $$$<span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">Returns Bar</span> $$$$<span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">Returns a</span><span style="color: #000000;"> $$$$$</span><span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">Returns Hello</span> $$$$$$<span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;">Returns World //... and so on ...//</span>?>
Example #3 可变属性示例
<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;"> Given these variables ...</span><span style="color: #800080;">$nameTypes</span> = <span style="color: #0000ff;">array</span>("first", "last", "company"<span style="color: #000000;">);</span><span style="color: #800080;">$name_first</span> = "John"<span style="color: #000000;">;</span><span style="color: #800080;">$name_last</span> = "Doe"<span style="color: #000000;">;</span><span style="color: #800080;">$name_company</span> = "PHP.net"<span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;"> Then this loop is ...</span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$nameTypes</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$type</span><span style="color: #000000;">) </span><span style="color: #0000ff;">print</span> ${"name_<span style="color: #800080;">$type</span>"} . "\n"<span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;"> ... equivalent to this print statement.</span><span style="color: #0000ff;">print</span> "<span style="color: #800080;">$name_first</span>\n<span style="color: #800080;">$name_last</span>\n<span style="color: #800080;">$name_company</span>\n"<span style="color: #000000;">;</span>?>
以上例程会输出:
<span style="color: #000000;">JohnDoePHP</span>.<span style="color: #000000;">netJohnDoePHP</span>.net
摘自:http://php.net/manual/zh/functions.variable-functions.php