Home  >  Article  >  Backend Development  >  Variable variable names in PHP_PHP tutorial

Variable variable names in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:51:53903browse

Sometimes variable variable names bring great convenience to programming. That is to say, variable names can be named and used dynamically. Usually variables are named with statements like the following:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>$<span class="attribute">a</span><span> = </span><span class="attribute-value">'hello'</span><span>; </span></span></li><li class="alt"><span><span class="tag">?></span><span> </span></span></li></ol>

Variable variable name refers to using the value of a variable as the name of the variable. In the example above, you can set hello to the name of a variable by using two $ signs, like below.

<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>$$<span class="attribute">a</span><span> = </span><span class="attribute-value">'world'</span><span>; </span></span></li><li class="alt"><span><span class="tag">?></span><span> </span></span></li></ol>

Through the above two statements, two variables are defined: variable $a, which contains "hello" and variable $hello, which contains "world". So, the following language:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>echo "$a ${$a}"; </span></li><li class="alt"><span><span class="tag">?></span><span> </span></span></li></ol>

The output is exactly the same as the following statement:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>echo "$a $hello"; </span></li><li class="alt"><span><span class="tag">?></span><span> </span></span></li></ol>

They all output: hello world.

In order to use mutable variable names for arrays, you need to resolve an ambiguity problem. That is, if you write $$a[1], the parser needs to understand whether you mean to treat $a[1] as a variable, or to treat $$a as a variable. [1] refers to this variable. index. The syntax to resolve this ambiguity is: use ${$a[1]} in the first case and ${$a}[1] in the second case.

Class properties can also be accessed via mutable property names. Mutable property names are taken from the access scope of the variable in which the call was made. For example, if your expression is like this: $foo->$bar, then the runtime will look for the variable $bar in the local variable scope, and its value will be used as a property name of the $foo object. It can also be used if $bar is an array.

Example 1 Variable variable name

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>class foo { </span></li><li class="alt"><span>    var $<span class="attribute">bar</span><span> = </span><span class="attribute-value">'I am bar.'</span><span>; </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span>$<span class="attribute">foo</span><span> = </span><span class="attribute-value">new</span><span> foo(); </span></span></li><li class="alt"><span>$<span class="attribute">bar</span><span> = </span><span class="attribute-value">'bar'</span><span>; </span></span></li><li><span>$<span class="attribute">baz</span><span> = </span><span class="attribute-value">array</span><span>('foo', 'bar', 'baz', 'quux'); </span></span></li><li class="alt"><span>echo $foo-<span class="tag">></span><span>$bar . "n"; </span></span></li>
<li><span>echo $foo-<span class="tag">></span><span>$baz[1] . "n"; </span></span></li>
<li class="alt"><span><span class="tag">?></span><span> </span></span></li>
</ol>

The above example will output the following results:

I am bar.

I am bar.

Warning

Please note that variable variable names cannot be used for super global array variables in PHP functions and classes. The variable $this is also a special variable that cannot be dynamically named.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371672.htmlTechArticleSometimes variable variable names bring great convenience to programming. That is to say, variable names can be named and used dynamically. Usually variables are named through the following statement: ? php $...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn