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 &gt; (bigger than), &gt;= (bigger than or equal to), == (equal),    != (not equal), &lt; (smaller than) and &lt;= (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&#039;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 &#039;$a++&#039; or &#039;++$a&#039;.    But what if you want to add more than one to it, for instance 3?    You could write &#039;$a++&#039; multiple times, but this is obviously not a    very efficient or comfortable way.  A much more common practice is    to write &#039;$a = $a + 3&#039;.  &#039;$a + 3&#039; 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 &#039;$a += 3&#039;.  This means exactly    &quot;take the value of $a, add 3 to it, and assign it back into $a&quot;.    In addition to being shorter and clearer, this also results in    faster execution.  The value of &#039;$a += 3&#039;, 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&#039;s    assigned into $a).  Any two-place operator can be used in this    operator-assignment mode, for example &#039;$a -= 5&#039; (subtract 5 from    the value of $a), &#039;$b *= 7&#039; (multiply the value of $b by 7), etc.   </p>   <p class="para">    There is one more expression that may seem odd if you haven&#039;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">&lt;?php<br />$first&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">$second&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$third<br />?&gt;</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">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">double</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;assign&nbsp;the&nbsp;value&nbsp;five&nbsp;into&nbsp;the&nbsp;variable&nbsp;$a&nbsp;and&nbsp;$b&nbsp;*/<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;post-increment,&nbsp;assign&nbsp;original&nbsp;value&nbsp;of&nbsp;$a&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(5)&nbsp;to&nbsp;$c&nbsp;*/<br /></span><span style="color: #0000BB">$e&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;++</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;pre-increment,&nbsp;assign&nbsp;the&nbsp;incremented&nbsp;value&nbsp;of&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$b&nbsp;(6)&nbsp;to&nbsp;$d&nbsp;and&nbsp;$e&nbsp;*/<br /><br />/*&nbsp;at&nbsp;this&nbsp;point,&nbsp;both&nbsp;$d&nbsp;and&nbsp;$e&nbsp;are&nbsp;equal&nbsp;to&nbsp;6&nbsp;*/<br /><br /></span><span style="color: #0000BB">$f&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">double</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">++);&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;assign&nbsp;twice&nbsp;the&nbsp;value&nbsp;of&nbsp;$d&nbsp;before<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;increment,&nbsp;2*6&nbsp;=&nbsp;12&nbsp;to&nbsp;$f&nbsp;*/<br /></span><span style="color: #0000BB">$g&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">double</span><span style="color: #007700">(++</span><span style="color: #0000BB">$e</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;assign&nbsp;twice&nbsp;the&nbsp;value&nbsp;of&nbsp;$e&nbsp;after<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;increment,&nbsp;2*7&nbsp;=&nbsp;14&nbsp;to&nbsp;$g&nbsp;*/<br /></span><span style="color: #0000BB">$h&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$g&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;first,&nbsp;$g&nbsp;is&nbsp;incremented&nbsp;by&nbsp;10&nbsp;and&nbsp;ends&nbsp;with&nbsp;the&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;of&nbsp;24.&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;assignment&nbsp;(24)&nbsp;is&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;assigned&nbsp;into&nbsp;$h,&nbsp;and&nbsp;$h&nbsp;ends&nbsp;with&nbsp;the&nbsp;value&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of&nbsp;24&nbsp;as&nbsp;well.&nbsp;*/<br /></span><span style="color: #0000BB">?&gt;</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 &#039;expr&#039; &#039;;&#039; that is, an    expression followed by a semicolon.  In &#039;$b=$a=5;&#039;, $a=5 is a    valid expression, but it&#039;s not a statement by itself.  &#039;$b=$a=5;&#039;    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&#039;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&#039;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 + -
显示快捷键?