language.expressions.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 206 行 · 第 1/2 页
HTML
206 行
expressions. These expressions evaluate to either <b><tt>FALSE</tt></b> or <b><tt>TRUE</tt></b>. PHP supports > (bigger than), >= (bigger than or equal to), == (equal), != (not equal), < (smaller than) and <= (smaller than or equal to). The language also supports a set of strict equivalence operators: === (equal to and same type) and !== (not equal to or not same type). These expressions are most commonly used inside conditional execution, such as <i>if</i> statements. </p> <p class="simpara"> The last example of expressions we'll deal with here is combined operator-assignment expressions. You already know that if you want to increment $a by 1, you can simply write '$a++' or '++$a'. But what if you want to add more than one to it, for instance 3? You could write '$a++' multiple times, but this is obviously not a very efficient or comfortable way. A much more common practice is to write '$a = $a + 3'. '$a + 3' evaluates to the value of $a plus 3, and is assigned back into $a, which results in incrementing $a by 3. In PHP, as in several other languages like C, you can write this in a shorter way, which with time would become clearer and quicker to understand as well. Adding 3 to the current value of $a can be written '$a += 3'. This means exactly "take the value of $a, add 3 to it, and assign it back into $a". In addition to being shorter and clearer, this also results in faster execution. The value of '$a += 3', like the value of a regular assignment, is the assigned value. Notice that it is NOT 3, but the combined value of $a plus 3 (this is the value that's assigned into $a). Any two-place operator can be used in this operator-assignment mode, for example '$a -= 5' (subtract 5 from the value of $a), '$b *= 7' (multiply the value of $b by 7), etc. </p> <p class="para"> There is one more expression that may seem odd if you haven't seen it in other languages, the ternary conditional operator: </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 />$first </span><span style="color: #007700">? </span><span style="color: #0000BB">$second </span><span style="color: #007700">: </span><span style="color: #0000BB">$third<br />?></span></span></code></div> </div> </div> </p> <p class="para"> If the value of the first subexpression is <b><tt>TRUE</tt></b> (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value. </p> <p class="para"> The following example should help you understand pre- and post-increment and expressions in general a bit better: </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 /></span><span style="color: #007700">function </span><span style="color: #0000BB">double</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">)<br />{<br /> return </span><span style="color: #0000BB">$i</span><span style="color: #007700">*</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </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">/* assign the value five into the variable $a and $b */<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">$a</span><span style="color: #007700">++; </span><span style="color: #FF8000">/* post-increment, assign original value of $a <br /> (5) to $c */<br /></span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">$d </span><span style="color: #007700">= ++</span><span style="color: #0000BB">$b</span><span style="color: #007700">; </span><span style="color: #FF8000">/* pre-increment, assign the incremented value of <br /> $b (6) to $d and $e */<br /><br />/* at this point, both $d and $e are equal to 6 */<br /><br /></span><span style="color: #0000BB">$f </span><span style="color: #007700">= </span><span style="color: #0000BB">double</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">++); </span><span style="color: #FF8000">/* assign twice the value of $d before<br /> the increment, 2*6 = 12 to $f */<br /></span><span style="color: #0000BB">$g </span><span style="color: #007700">= </span><span style="color: #0000BB">double</span><span style="color: #007700">(++</span><span style="color: #0000BB">$e</span><span style="color: #007700">); </span><span style="color: #FF8000">/* assign twice the value of $e after<br /> the increment, 2*7 = 14 to $g */<br /></span><span style="color: #0000BB">$h </span><span style="color: #007700">= </span><span style="color: #0000BB">$g </span><span style="color: #007700">+= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #FF8000">/* first, $g is incremented by 10 and ends with the <br /> value of 24. the value of the assignment (24) is <br /> then assigned into $h, and $h ends with the value <br /> of 24 as well. */<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> Some expressions can be considered as statements. In this case, a statement has the form of 'expr' ';' that is, an expression followed by a semicolon. In '$b=$a=5;', $a=5 is a valid expression, but it's not a statement by itself. '$b=$a=5;' however is a valid statement. </p> <p class="simpara"> One last thing worth mentioning is the truth value of expressions. In many events, mainly in conditional execution and loops, you're not interested in the specific value of the expression, but only care about whether it means <b><tt>TRUE</tt></b> or <b><tt>FALSE</tt></b>. The constants <b><tt>TRUE</tt></b> and <b><tt>FALSE</tt></b> (case-insensitive) are the two possible boolean values. When necessary, an expression is automatically converted to boolean. See the <a href="language.types.type-juggling.html#language.types.typecasting" class="link">section about type-casting</a> for details about how. </p> <p class="simpara"> PHP provides a full and powerful implementation of expressions, and documenting it entirely goes beyond the scope of this manual. The above examples should give you a good idea about what expressions are and how you can construct useful expressions. Throughout the rest of this manual we'll write <var class="varname">expr</var> to indicate any valid PHP expression. </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.constants.predefined.html">Magic constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.html">Operators</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 + -
显示快捷键?