<!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: '<em>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</em>' </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'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"><?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: #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<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">?></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'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, "becomes an alias for" or "points to") 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 (&) to the beginning of the variable which is being assigned (the source variable). For instance, the following code snippet outputs '<em>My name is Bob</em>' twice: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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">= &</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">?></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"><?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">= &</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">= &(</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">= &</span><span style="color: #0000BB">test</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Invalid.<br /></span><span style="color: #0000BB">?></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"><?php<br /></span><span style="color: #FF8000">// Unset AND unreferenced (no use context) variable; outputs 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">// Boolean usage; outputs 'false' (See ternary operators for more on this syntax)<br /></span><span style="color: #007700">echo(</span><span style="color: #0000BB">$unset_bool </span><span style="color: #007700">? </span><span style="color: #DD0000">"true\n" </span><span style="color: #007700">: </span><span style="color: #DD0000">"false\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// String usage; outputs 'string(3) "abc"'<br /></span><span style="color: #0000BB">$unset_str </span><span style="color: #007700">.= </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">// Integer usage; outputs 'int(25)'<br /></span><span style="color: #0000BB">$unset_int </span><span style="color: #007700">+= </span><span style="color: #0000BB">25</span><span style="color: #007700">; </span><span style="color: #FF8000">// 0 + 25 => 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">// Float/double usage; outputs 'float(1.25)'<br /></span><span style="color: #0000BB">$unset_float </span><span style="color: #007700">+= </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">// Array usage; outputs array(1) { [3]=> string(3) "def" }<br /></span><span style="color: #0000BB">$unset_arr</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">] = </span><span style="color: #DD0000">"def"</span><span style="color: #007700">; </span><span style="color: #FF8000">// array() + array(3 => "def") => array(3 => "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">// Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)<br />// Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" }<br /></span><span style="color: #0000BB">$unset_obj</span><span style="color: #007700">-></span><span style="color: #0000BB">foo </span><span style="color: #007700">= </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">?></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>