<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.variables.basics.php',
    1 => 'Basics',
    2 => 'Basics',
  ),
  'up' => 
  array (
    0 => 'language.variables.php',
    1 => 'Variables',
  ),
  'prev' => 
  array (
    0 => 'language.variables.php',
    1 => 'Variables',
  ),
  'next' => 
  array (
    0 => 'language.variables.predefined.php',
    1 => 'Predefined Variables',
  ),
  '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.basics" class="sect1">
   <h2 class="title">Basics</h2>

   <p class="simpara">
    Variables in PHP are represented by a dollar sign followed by the
    name of the variable. The variable name is case-sensitive.
   </p>

   <p class="para">
    A valid variable name starts with a letter
    (<code class="literal">A-Z</code>, <code class="literal">a-z</code>, or the bytes from 128 through 255)
    or underscore, followed
    by any number of letters, numbers, or underscores. As a regular
    expression, it would be expressed thus:
    <code class="code">^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code>
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     PHP doesn&#039;t support Unicode variable names, however, some character
     encodings (such as UTF-8) encode characters in such a way that all bytes
     of a multi-byte character fall within the allowed range, thus making it a
     valid variable name.
    </span>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     <code class="literal">$this</code> is a special variable that can&#039;t be
     assigned.
     Prior to PHP 7.1.0, indirect assignment (e.g. by using
     <a href="language.variables.variable.php" class="link">variable variables</a>)
     was possible.
    </span>
   </p></blockquote>

   <div class="tip"><strong class="tip">Tip</strong><p class="simpara">See also the
<a href="userlandnaming.php" class="xref">Userland Naming Guide</a>.</p></div>

   <div class="example" id="example-1">
    <p><strong>Example #1 Valid variable names</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$var </span><span style="color: #007700">= </span><span style="color: #DD0000">'Bob'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$Var </span><span style="color: #007700">= </span><span style="color: #DD0000">'Joe'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$var</span><span style="color: #DD0000">, </span><span style="color: #0000BB">$Var</span><span style="color: #DD0000">"</span><span style="color: #007700">;      </span><span style="color: #FF8000">// outputs "Bob, Joe"<br /><br /></span><span style="color: #0000BB">$_4site </span><span style="color: #007700">= </span><span style="color: #DD0000">'not yet'</span><span style="color: #007700">;    </span><span style="color: #FF8000">// valid; starts with an underscore<br /></span><span style="color: #0000BB">$täyte </span><span style="color: #007700">= </span><span style="color: #DD0000">'mansikka'</span><span style="color: #007700">;    </span><span style="color: #FF8000">// valid; 'ä' is (Extended) ASCII 228.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <div class="example" id="example-2">
    <p><strong>Example #2 Invalid variable names</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">$</span><span style="color: #0000BB">4site </span><span style="color: #007700">= </span><span style="color: #DD0000">'not yet'</span><span style="color: #007700">;     </span><span style="color: #FF8000">// invalid; starts with a number</span></span></code></div>
    </div>

   </div>

   <p class="simpara">
    PHP accepts a sequence of any bytes as a variable name. Variable
    names that do not follow the above-mentioned naming rules can only be
    accessed dynamically at runtime. See
    <a href="language.variables.variable.php" class="link">variable variables</a>
    for information on how to access them.
   </p>

   <div class="example" id="example-3">
    <p><strong>Example #3 Accessing obscure variable names</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">${</span><span style="color: #DD0000">'invalid-name'</span><span style="color: #007700">} = </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'invalid-name'</span><span style="color: #007700">;<br />echo ${</span><span style="color: #DD0000">'invalid-name'</span><span style="color: #007700">}, </span><span style="color: #DD0000">" "</span><span style="color: #007700">, $</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<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">
<div class="cdata"><pre>
bar bar
</pre></div>
    </div>
   </div>

   <p class="para">
    By default, variables are always assigned by value. That is to say,
    when an expression is assigned to a variable, the entire value of
    the original expression is copied into the destination
    variable. This means, for instance, that after assigning one
    variable&#039;s value to another, changing one of those variables will
    have no effect on the other. For more information on this kind of
    assignment, see the chapter on <a href="language.expressions.php" class="link">Expressions</a>.
   </p>
   <p class="para">
    PHP also offers another way to assign values to variables:
    <a href="language.references.php" class="link">assign by reference</a>. 
    This means that the new variable simply references (in other words, 
    &quot;becomes an alias for&quot; or &quot;points to&quot;) the original variable. 
    Changes to the new variable affect the original, and vice versa. 
   </p>
   <p class="para">
    To assign by reference, simply prepend an ampersand (&amp;) to the
    beginning of the variable which is being assigned (the source
    variable). For instance, the following code snippet outputs &#039;<code class="literal">My
    name is Bob</code>&#039; twice:

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'Bob'</span><span style="color: #007700">;              </span><span style="color: #FF8000">// Assign the value 'Bob' to $foo<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;              </span><span style="color: #FF8000">// Reference $foo via $bar.<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">"My name is </span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">"</span><span style="color: #007700">;  </span><span style="color: #FF8000">// Alter $bar...<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;                 </span><span style="color: #FF8000">// $foo is altered too.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>

   <p class="para">
    One important thing to note is that only variables may be
    assigned by reference.
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">25</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;      </span><span style="color: #FF8000">// This is a valid assignment.<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &amp;(</span><span style="color: #0000BB">24 </span><span style="color: #007700">* </span><span style="color: #0000BB">7</span><span style="color: #007700">);  </span><span style="color: #FF8000">// Invalid; references an unnamed expression.<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">()<br />{<br />   return </span><span style="color: #0000BB">25</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">test</span><span style="color: #007700">();    </span><span style="color: #FF8000">// Invalid because test() doesn't return a variable by reference.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   
   <p class="para">
    It is not necessary to declare variables in PHP, however, it is a very
    good practice. Accessing an undefined variable will result in an
    <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> (prior to PHP 8.0.0, <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>).
    An undefined variable has a default value of <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
    The <span class="function"><a href="function.isset.php" class="function">isset()</a></span> language
    construct can be used to detect if a variable has already been initialized.
   </p>
   <p class="para">
    <div class="example" id="example-4">
     <p><strong>Example #4 Default value of an uninitialized variable</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: #FF8000">// Unset AND unreferenced (no use context) variable.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_var</span><span style="color: #007700">);<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">
<div class="cdata"><pre>
Warning: Undefined variable $unset_var in ...
NULL
</pre></div>
     </div>
    </div>
   </p>

   <p class="simpara">
    PHP allows array autovivification (automatic creation of new arrays)
    from an undefined variable.
    Appending an element to an undefined variable will create a new array and
    will not generate a warning.
   </p>
   <div class="example" id="example-5">
    <p><strong>Example #5 Autovivification of an array from an undefined variable</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$unset_array</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'value'</span><span style="color: #007700">; </span><span style="color: #FF8000">// Does not generate a warning.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     Relying on the default value of an uninitialized variable is problematic
     when including one file in another which uses the same
     variable name.
    </p>
   </div>

   <p class="simpara">
    A variable can be destroyed by using the <span class="function"><a href="function.unset.php" class="function">unset()</a></span>
    language construct.
   </p>

   <p class="simpara">
    For information on variable-related functions, see the
    <a href="ref.var.php" class="link">Variable Functions Reference</a>.
   </p>
  </div><?php manual_footer($setup); ?>