language.expressions.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 206 行 · 第 1/2 页

HTML
206
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Expressions</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.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><hr /><div>   <h1>Expressions</h1>   <p class="simpara">    Expressions are the most important building stones of PHP.  In PHP,    almost anything you write is an expression.  The simplest yet    most accurate way to define an expression is &quot;anything that has a    value&quot;.   </p>   <p class="simpara">    The most basic forms of expressions are constants and variables.    When you type &quot;$a = 5&quot;, you&#039;re assigning &#039;5&#039; into $a.  &#039;5&#039;, obviously,    has the value 5, or in other words &#039;5&#039; is an expression with the    value of 5 (in this case, &#039;5&#039; is an integer constant).   </p>   <p class="simpara">    After this assignment, you&#039;d expect $a&#039;s value to be 5 as    well, so if you wrote $b = $a, you&#039;d expect it to behave just as    if you wrote $b = 5.  In other words, $a is an expression with the    value of 5 as well.  If everything works right, this is exactly    what will happen.   </p>   <p class="para">    Slightly more complex examples for expressions are functions.  For    instance, consider the following function:    <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">foo&nbsp;</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>    </div>   </p>   <p class="simpara">    Assuming you&#039;re familiar with the concept of functions (if you&#039;re    not, take a look at the chapter about <a href="language.functions.html" class="link">functions</a>), you&#039;d assume    that typing <i>$c = foo()</i> is essentially just like    writing <i>$c = 5</i>, and you&#039;re right.  Functions    are expressions with the value of their return value.  Since foo()    returns 5, the value of the expression &#039;foo()&#039; is 5.  Usually    functions don&#039;t just return a static value but compute something.   </p>   <p class="simpara">    Of course, values in PHP don&#039;t have to be integers, and very often    they aren&#039;t.  PHP supports four scalar value types: <a href="language.types.integer.html" class="type integer">integer</a>    values, floating point values (<a href="language.types.float.html" class="type float">float</a>), <a href="language.types.string.html" class="type string">string</a>    values and <a href="language.types.boolean.html" class="type boolean">boolean</a> values (scalar values are values that you    can&#039;t &#039;break&#039; into smaller pieces, unlike arrays, for instance). PHP also     supports two composite (non-scalar) types: arrays and objects. Each of    these value types can be assigned into variables or returned from functions.   </p>   <p class="simpara">    PHP takes expressions much further, in the same way many other languages    do.  PHP is an expression-oriented language, in the    sense that almost everything is an expression.  Consider the    example we&#039;ve already dealt with, &#039;$a = 5&#039;.  It&#039;s easy to see that    there are two values involved here, the value of the integer    constant &#039;5&#039;, and the value of $a which is being updated to 5 as    well.  But the truth is that there&#039;s one additional value involved    here, and that&#039;s the value of the assignment itself.  The    assignment itself evaluates to the assigned value, in this case 5.    In practice, it means that &#039;$a = 5&#039;, regardless of what it does,    is an expression with the value 5.  Thus, writing something like    &#039;$b = ($a = 5)&#039; is like writing &#039;$a = 5; $b = 5;&#039; (a semicolon    marks the end of a statement).  Since assignments are parsed in a    right to left order, you can also write &#039;$b = $a = 5&#039;.   </p>   <p class="simpara">    Another good example of expression orientation is pre- and    post-increment and decrement.  Users of PHP and many other    languages may be familiar with the notation of variable++ and    variable--.  These are <a href="language.operators.increment.html" class="link">    increment and decrement operators</a>.  In    PHP/FI 2, the statement &#039;$a++&#039; has no value (is not an    expression), and thus you can&#039;t assign it or use it in any way.    PHP enhances the increment/decrement capabilities by making    these expressions as well, like in C.  In PHP, like in C, there    are two types of increment - pre-increment and post-increment.    Both pre-increment and post-increment essentially increment the    variable, and the effect on the variable is identical.  The    difference is with the value of the increment expression.    Pre-increment, which is written &#039;++$variable&#039;, evaluates to the    incremented value (PHP increments the variable before reading its    value, thus the name &#039;pre-increment&#039;).  Post-increment, which is    written &#039;$variable++&#039; evaluates to the original value of    $variable, before it was incremented (PHP increments the variable    after reading its value, thus the name &#039;post-increment&#039;).   </p>   <p class="simpara">    A very common type of expressions are <a href="language.operators.comparison.html" class="link">comparison</a>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?