<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.variables.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'language.variables.variable.php',
    1 => 'Variable variables',
    2 => 'Variable variables',
  ),
  'up' => 
  array (
    0 => 'language.variables.php',
    1 => 'Variables',
  ),
  'prev' => 
  array (
    0 => 'language.variables.scope.php',
    1 => 'Variable scope',
  ),
  'next' => 
  array (
    0 => 'language.variables.external.php',
    1 => 'Variables From External Sources',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/variables.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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 </span><span style="color: #007700">= </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>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 </span><span style="color: #007700">= </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">$a</var> with contents &quot;hello&quot; and
    <var class="varname">$hello</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 </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000"> </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 </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000"> </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,
    an ambiguity problem has to be resolved. That is, if the parser sees
    <var class="varname">$$a[1]</var> then it needs to know if
    <var class="varname">$a[1]</var> was meant to be used as a variable, or if
    <var class="varname">$$a</var> was wanted as the variable and then the <code class="literal">[1]</code>
    index from that variable. The syntax for resolving this ambiguity
    is: <var class="varname">${$a[1]}</var> for the first case and
    <var class="varname">${$a}[1]</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 there is an expression such as
    <var class="varname">$foo->$bar</var>, then the local scope will be examined for
    <var class="varname">$bar</var> and its value will be used as the name of the
    property of <var class="varname">$foo</var>. This is also true if
    <var class="varname">$bar</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 multiple parts,
    or when the property name contains characters that are not
    otherwise valid (e.g. from <span class="function"><a href="function.json-decode.php" class="function">json_decode()</a></span>
    or <a href="book.simplexml.php" class="link">SimpleXML</a>).
   </p>

   <p class="para">
    <div class="example" id="example-1">
     <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 </span><span style="color: #0000BB">Foo </span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">'I am bar.'</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">$arr </span><span style="color: #007700">= [</span><span style="color: #DD0000">'I am A.'</span><span style="color: #007700">, </span><span style="color: #DD0000">'I am B.'</span><span style="color: #007700">, </span><span style="color: #DD0000">'I am C.'</span><span style="color: #007700">];<br />    public </span><span style="color: #0000BB">$r   </span><span style="color: #007700">= </span><span style="color: #DD0000">'I am r.'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$baz </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'baz'</span><span style="color: #007700">, </span><span style="color: #DD0000">'quux'</span><span style="color: #007700">];<br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$bar </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </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">]} . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$start </span><span style="color: #007700">= </span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$end   </span><span style="color: #007700">= </span><span style="color: #DD0000">'ar'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">$start </span><span style="color: #007700">. </span><span style="color: #0000BB">$end</span><span style="color: #007700">} . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$arr </span><span style="color: #007700">= </span><span style="color: #DD0000">'arr'</span><span style="color: #007700">;<br />echo </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">]} . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </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">] . </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>Il precedente esempio visualizzerà:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
I am bar.
I am bar.
I am bar.
I am r.
I am B.
</pre></div>
     </div>
    </div>
   </p>

   <div class="warning"><strong class="warning">Avviso</strong>
    <p class="simpara">
     Please note that variable variables cannot be used with PHP&#039;s 
     <a href="language.variables.superglobals.php" class="link">Superglobal arrays</a>
     within functions or class methods. The variable <code class="literal">$this</code>
     is also a special variable that cannot be referenced dynamically.
    </p>
   </div>
  
  </div><?php manual_footer($setup); ?>