Maison >développement back-end >tutoriel php >PHP的学习--可变变量,--可变变量_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