<?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>: 
    <p class="simpara">
     Variable names are compared as bytes, not as characters. PHP does not
     interpret or validate the encoding of the bytes from 128 through 255, and
     two names refer to the same variable only when their bytes are identical.
    </p>
    <p class="simpara">
     One consequence is that names written in UTF-8 work, because every byte of
     a UTF-8 multi-byte sequence falls inside the accepted range. The same holds
     for any encoding whose non-ASCII bytes all fall in that range, and for byte
     sequences that are not valid text in any encoding. Encodings that use bytes
     below 128 within a multi-byte sequence, such as Shift-JIS or UTF-16, do not.
    </p>
    <p class="simpara">
     Another is that two names which display identically may be distinct.
     <code class="literal">ä</code> encoded as U+00E4 and as <code class="literal">a</code>
     followed by the combining diaeresis U+0308 are different variables, as are
     a name with and without a trailing U+00A0 NO-BREAK SPACE.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p 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.
    </p>
   </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="annotation-non-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$var </span><span style="color: #007700">= </span><span style="color: #DD0000">'Bob'</span><span style="color: #007700">;
</span><span style="color: #0000BB">$Var </span><span style="color: #007700">= </span><span style="color: #DD0000">'Joe'</span><span style="color: #007700">;
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"

</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
</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.</span></code></pre></div>
    </div>

   </div>

   <div class="example" id="example-2">
    <p><strong>Example #2 Invalid variable names</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</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></code></pre></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="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</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">;
</span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'invalid-name'</span><span style="color: #007700">;
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">;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive 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="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$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
</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.
</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...
</span><span style="color: #007700">echo </span><span style="color: #0000BB">$bar </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;
echo </span><span style="color: #0000BB">$foo </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;       </span><span style="color: #FF8000">// $foo is altered too.</span></code></pre></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="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$original </span><span style="color: #007700">= </span><span style="color: #0000BB">25</span><span style="color: #007700">;
</span><span style="color: #0000BB">$ref1 </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">$original</span><span style="color: #007700">;        </span><span style="color: #FF8000">// This is a valid assignment.
</span><span style="color: #0000BB">$ref2 </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.</span></code></pre></div>
     </div>

    </div>
    <div class="informalexample">
     <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #FF8000">// Note the lack of &amp; indicating a returned reference in the function declaration.
// i.e. the function does not return a reference, so the result cannot be assigned by reference
</span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">()
{
   </span><span style="color: #0000BB">$original </span><span style="color: #007700">= </span><span style="color: #0000BB">25</span><span style="color: #007700">;
   return </span><span style="color: #0000BB">$original</span><span style="color: #007700">;
}

</span><span style="color: #FF8000">// Note: A notice is issued, but the non-reference value is assigned
</span><span style="color: #0000BB">$result1 </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.
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result1</span><span style="color: #007700">);

</span><span style="color: #FF8000">// This function is defined as returning a reference, but does not return a variable
</span><span style="color: #007700">function &amp;</span><span style="color: #0000BB">test2</span><span style="color: #007700">()
{
    return </span><span style="color: #0000BB">26</span><span style="color: #007700">;             </span><span style="color: #FF8000">// Invalid because the returned value isn't a variable reference.
</span><span style="color: #007700">}

</span><span style="color: #FF8000">// Note: The non-reference value is assigned
</span><span style="color: #0000BB">$result2 </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">test2</span><span style="color: #007700">();
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result2</span><span style="color: #007700">);</span></code></pre></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="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #FF8000">// Unset AND unreferenced (no use context) variable.
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_var</span><span style="color: #007700">);</span></code></pre></div>
     </div>

     <div class="example-contents"><p>The above example will output:</p></div>
     <div class="example-contents screen">
<div class="annotation-interactive 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="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$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.
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_array</span><span style="color: #007700">);</span></code></pre></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); ?>