language.variables.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 158 行
HTML
158 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Variables</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.type-juggling.html">Type Juggling</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="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Variables</h1><h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="language.variables.predefined.html">Predefined Variables</a></li><li><a href="language.variables.scope.html">Variable scope</a></li><li><a href="language.variables.variable.html">Variable variables</a></li><li><a href="language.variables.external.html">Variables From External Sources</a></li></ul> <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: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff). </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> <i>$this</i> is a special variable that can't be assigned. </span> </p></blockquote> <div class="tip"><b class="tip">Tip</b><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">"$var, $Var"</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 'My name is Bob' 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 $bar"</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 <b><tt>FALSE</tt></b>, integers and floats default to zero, strings (e.g. used in <a href="function.echo.html" class="function">echo()</a>) are set as an empty string and arrays become to an empty array. </p> <p class="para"> <div class="example"> <p><b>Example #1 Default values of uninitialized variables</b></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="errorfunc.constants.html#errorfunc.constants.errorlevels.e-notice" 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. <a href="function.isset.html" class="function">isset()</a> language construct can be used to detect if a variable has been already initialized. </p> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.type-juggling.html">Type Juggling</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="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?