Sophie

Sophie

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

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>Basics</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.html">Variables</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.variables.predefined.html">Predefined Variables</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.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">
    Variable names follow the same rules as other labels in PHP. A
    valid variable name starts with a letter or underscore, followed
    by any number of letters, numbers, or underscores. As a regular
    expression, it would be expressed thus:
    &#039;<em>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</em>&#039;
   </p>
   
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     For our purposes here, a letter is a-z, A-Z, and the bytes
     from 127 through 255 (<em>0x7f-0xff</em>).
    </span>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     <em>$this</em> is a special variable that can&#039;t be
     assigned.
    </span>
   </p></blockquote>

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

   <p class="para">
    For information on variable related functions, see the
    <a href="ref.var.html" class="link">Variable Functions Reference</a>.
   </p>

   <p class="para">
    <div class="informalexample">
     <div class="example-contents"> 
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Bob'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$Var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Joe'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$var</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #0000BB">$Var</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;outputs&nbsp;"Bob,&nbsp;Joe"<br /><br /></span><span style="color: #007700">$</span><span style="color: #0000BB">4site&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'not&nbsp;yet'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;invalid;&nbsp;starts&nbsp;with&nbsp;a&nbsp;number<br /></span><span style="color: #0000BB">$_4site&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'not&nbsp;yet'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;valid;&nbsp;starts&nbsp;with&nbsp;an&nbsp;underscore<br /></span><span style="color: #0000BB">$täyte&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'mansikka'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;valid;&nbsp;'ä'&nbsp;is&nbsp;(Extended)&nbsp;ASCII&nbsp;228.<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <p class="para">
    By default, variables are always assigned by value. That is to say,
    when you assign an expression 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.html" class="link">Expressions</a>.
   </p>
   <p class="para">
    PHP also offers another way to assign values to variables:
    <a href="language.references.html" 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;<em>My
    name is Bob</em>&#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&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Bob'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Assign&nbsp;the&nbsp;value&nbsp;'Bob'&nbsp;to&nbsp;$foo<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Reference&nbsp;$foo&nbsp;via&nbsp;$bar.<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"My&nbsp;name&nbsp;is&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Alter&nbsp;$bar...<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;altered&nbsp;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 named 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&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">25</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;This&nbsp;is&nbsp;a&nbsp;valid&nbsp;assignment.<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&amp;(</span><span style="color: #0000BB">24&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Invalid;&nbsp;references&nbsp;an&nbsp;unnamed&nbsp;expression.<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">25</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">test</span><span style="color: #007700">();&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Invalid.<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   
   <p class="para">
    It is not necessary to initialize variables in PHP however it is a very
    good practice. Uninitialized variables have a default value of their type depending on the context in which they are used
    - booleans default to <strong><code>FALSE</code></strong>, integers and floats default to zero, strings (e.g. used in  <span class="function"><a href="function.echo.html" class="function">echo</a></span>) are 
    set as an empty string and arrays become to an empty array.
   </p>
   <p class="para">
    <div class="example" id="example-99">
     <p><strong>Example #1 Default values of uninitialized variables</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">//&nbsp;Unset&nbsp;AND&nbsp;unreferenced&nbsp;(no&nbsp;use&nbsp;context)&nbsp;variable;&nbsp;outputs&nbsp;NULL<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 /><br /></span><span style="color: #FF8000">//&nbsp;Boolean&nbsp;usage;&nbsp;outputs&nbsp;'false'&nbsp;(See&nbsp;ternary&nbsp;operators&nbsp;for&nbsp;more&nbsp;on&nbsp;this&nbsp;syntax)<br /></span><span style="color: #007700">echo(</span><span style="color: #0000BB">$unset_bool&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">"true\n"&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">"false\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;String&nbsp;usage;&nbsp;outputs&nbsp;'string(3)&nbsp;"abc"'<br /></span><span style="color: #0000BB">$unset_str&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'abc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_str</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Integer&nbsp;usage;&nbsp;outputs&nbsp;'int(25)'<br /></span><span style="color: #0000BB">$unset_int&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">25</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0&nbsp;+&nbsp;25&nbsp;=&gt;&nbsp;25<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_int</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Float/double&nbsp;usage;&nbsp;outputs&nbsp;'float(1.25)'<br /></span><span style="color: #0000BB">$unset_float&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">1.25</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_float</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Array&nbsp;usage;&nbsp;outputs&nbsp;array(1)&nbsp;{&nbsp;&nbsp;[3]=&gt;&nbsp;&nbsp;string(3)&nbsp;"def"&nbsp;}<br /></span><span style="color: #0000BB">$unset_arr</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"def"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;array()&nbsp;+&nbsp;array(3&nbsp;=&gt;&nbsp;"def")&nbsp;=&gt;&nbsp;array(3&nbsp;=&gt;&nbsp;"def")<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_arr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Object&nbsp;usage;&nbsp;creates&nbsp;new&nbsp;stdClass&nbsp;object&nbsp;(see&nbsp;http://www.php.net/manual/en/reserved.classes.php)<br />//&nbsp;Outputs:&nbsp;object(stdClass)#1&nbsp;(1)&nbsp;{&nbsp;&nbsp;["foo"]=&gt;&nbsp;&nbsp;string(3)&nbsp;"bar"&nbsp;}<br /></span><span style="color: #0000BB">$unset_obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$unset_obj</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    Relying on the default value of an uninitialized variable is problematic
    in the case of including one file into another which uses the same
    variable name. It is also a major <a href="security.globals.html" class="link">security risk</a> with <a href="ini.core.html#ini.register-globals" class="link">register_globals</a> turned on. <a href="" class="link">E_NOTICE</a> level error is issued in case of
    working with uninitialized variables, however not in the case of appending
    elements to the uninitialized array.  <span class="function"><a href="function.isset.html" class="function">isset()</a></span> language
    construct can be used to detect if a variable has been already initialized.
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.html">Variables</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.variables.predefined.html">Predefined Variables</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>