📄 language.operators.assignment.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Assignment Operators</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.operators.arithmetic.html">Arithmetic Operators</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.bitwise.html">Bitwise Operators</a></div> <div class="up"><a href="language.operators.html">Operators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="language.operators.assignment" class="sect1"> <h2 class="title">Assignment Operators</h2> <p class="simpara"> The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the rights (that is, "gets set to"). </p> <p class="para"> The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$a </span><span style="color: #007700">= (</span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">) + </span><span style="color: #0000BB">5</span><span style="color: #007700">; </span><span style="color: #FF8000">// $a is equal to 9 now, and $b has been set to 4.<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> In addition to the basic assignment operator, there are "combined operators" for all of the <a href="language.operators.html" class="link">binary arithmetic</a>, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example: <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">+= </span><span style="color: #0000BB">5</span><span style="color: #007700">; </span><span style="color: #FF8000">// sets $a to 8, as if we had said: $a = $a + 5;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #DD0000">"Hello "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">.= </span><span style="color: #DD0000">"There!"</span><span style="color: #007700">; </span><span style="color: #FF8000">// sets $b to "Hello There!", just like $b = $b . "There!";<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. Assignment by reference is also supported, using the <span class="computeroutput">$var = &$othervar;</span> syntax. 'Assignment by reference' means that both variables end up pointing at the same data, and nothing is copied anywhere. To learn more about references, please read <a href="language.references.html" class="link">References explained</a>. As of PHP 5, objects are assigned by reference unless explicitly told otherwise with the new <a href="language.oop5.cloning.html" class="link">clone</a> keyword. </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.operators.arithmetic.html">Arithmetic Operators</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.bitwise.html">Bitwise Operators</a></div> <div class="up"><a href="language.operators.html">Operators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -