downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/language.variables.variable.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'language.variables.variable.php',
   
1 => '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',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<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 programlisting">
<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 programlisting">
<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">$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 programlisting">
<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 programlisting">
<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">$$a[1]</var> then the parser needs to know if you
    meant to use <var class="varname">$a[1]</var> as a variable, or if you
    wanted <var class="varname">$$a</var> as the variable and then the [1]
    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 you have 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="para">
    </p><div class="example">
     <p><b>Example #1 Variable function example</b></p>
      <div class="example-contents programlisting">
<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 />}<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 /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

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

   <div class="warning"><b class="warning">Warning</b>
    <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 <i>$this</i>
     is also a special variable that cannot be referenced dynamically.
    </p>
   </div>
 
  </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites