Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8976

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Variable variables</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.scope.html">Variable scope</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.variables.external.html">Variables From External Sources</a></div>
 <div class="up"><a href="language.variables.html">Variables</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.variables.variable" class="sect1">
   <h2 class="title">Variable variables</h2>

   <p class="simpara">
    Sometimes it is convenient to be able to have variable variable
    names.  That is, a variable name which can be set and used
    dynamically.  A normal variable is set with a statement such as:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    A variable variable takes the value of a variable and treats that
    as the name of a variable.  In the above example,
    <em class="emphasis">hello</em>, can be used as the name of a variable
    by using two dollar signs. i.e.
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">$</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'world'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    At this point two variables have been defined and stored in the
    PHP symbol tree: <var class="varname"><var class="varname">$a</var></var> with contents &quot;hello&quot; and
    <var class="varname"><var class="varname">$hello</var></var> with contents &quot;world&quot;.  Therefore, this
    statement:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">&nbsp;</span><span style="color: #007700">${</span><span style="color: #0000BB">$a</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    produces the exact same output as:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">&nbsp;</span><span style="color: #0000BB">$hello</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    i.e. they both produce: <span class="computeroutput">hello world</span>.
   </p>

   <p class="simpara">
    In order to use variable variables with arrays, you have to
    resolve an ambiguity problem.  That is, if you write
    <var class="varname"><var class="varname">$$a[1]</var></var> then the parser needs to know if you
    meant to use <var class="varname"><var class="varname">$a[1]</var></var> as a variable, or if you
    wanted <var class="varname"><var class="varname">$$a</var></var> as the variable and then the [1]
    index from that variable.  The syntax for resolving this ambiguity
    is: <var class="varname"><var class="varname">${$a[1]}</var></var> for the first case and
    <var class="varname"><var class="varname">${$a}[1]</var></var> for the second. 
   </p>

   <p class="simpara">
    Class properties may also be accessed using variable property
    names. The variable property name will be resolved within the
    scope from which the call is made. For instance, if you have an
    expression such as <var class="varname"><var class="varname">$foo->$bar</var></var>, then the local
    scope will be examined for <var class="varname"><var class="varname">$bar</var></var> and its value
    will be used as the name of the property
    of <var class="varname"><var class="varname">$foo</var></var>. This is also true
    if <var class="varname"><var class="varname">$bar</var></var> is an array access.
   </p>
   <p class="simpara">
    Curly braces may also be used, to clearly delimit the property
    name. They are most useful when accessing values within a property that
    contains an array, when the property name is made of mulitple parts,
    or when the property name contains characters that are not
    otherwise valid (e.g. from  <span class="function"><a href="function.json-decode.html" class="function">json_decode()</a></span>
    or <a href="book.simplexml.html" class="link">SimpleXML</a>).
   </p>

   <p class="para">
    <div class="example" id="example-107">
     <p><strong>Example #1 Variable property example</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;bar.'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'I&nbsp;am&nbsp;A.'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;B.'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;C.'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$r&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;r.'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$baz&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'quux'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$start&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$end&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'ar'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">$start&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$end</span><span style="color: #007700">}&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'arr'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">}[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>The above example will output:</p></div>
     <div class="example-contents screen"><br />
I am bar.<br />
I am bar.<br />
I am bar.<br />
I am r.<br />
I am B.<br />
     </div>
    </div>
   </p>

   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     Please note that variable variables cannot be used with PHP&#039;s 
     <a href="language.variables.superglobals.html" class="link">Superglobal arrays</a>
     within functions or class methods. The variable <em>$this</em>
     is also a special variable that cannot be referenced dynamically.
    </p>
   </div>
  
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.scope.html">Variable scope</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.variables.external.html">Variables From External Sources</a></div>
 <div class="up"><a href="language.variables.html">Variables</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>