File: expressions.xml

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (238 lines) | stat: -rw-r--r-- 10,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
 <chapter xml:id="language.expressions" xmlns="http://docbook.org/ns/docbook">
   <title>Expressions</title>
   <simpara>
    Expressions are the most important building blocks of PHP.  In PHP,
    almost anything you write is an expression.  The simplest yet
    most accurate way to define an expression is "anything that has a
    value".
   </simpara>
   <simpara>
    The most basic forms of expressions are constants and variables.
    When you type <code>$a = 5</code>, you're assigning <code>5</code> into
    <varname>$a</varname>.  <code>5</code>, obviously,
    has the value 5, or in other words <code>5</code> is an expression with the
    value of 5 (in this case, <code>5</code> is an integer constant).
   </simpara>
   <simpara>
    After this assignment, you'd expect <varname>$a</varname>'s value to be 5 as
    well, so if you wrote <code>$b = $a</code>, you'd expect it to behave just as
    if you wrote <code>$b = 5</code>.  In other words, <varname>$a</varname> is an expression with the
    value of 5 as well.  If everything works right, this is exactly
    what will happen.
   </simpara>
   <para>
    Slightly more complex examples for expressions are functions.  For
    instance, consider the following function:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
function foo ()
{
    return 5;
}
?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Assuming you're familiar with the concept of functions (if you're
    not, take a look at the chapter about <link
    linkend="language.functions">functions</link>), you'd assume
    that typing <code>$c = foo()</code> is essentially just like
    writing <code>$c = 5</code>, and you're right.  Functions
    are expressions with the value of their return value.  Since <code>foo()</code>
    returns 5, the value of the expression '<code>foo()</code>' is 5.  Usually
    functions don't just return a static value but compute something.
   </simpara>
   <simpara>
    Of course, values in PHP don't have to be integers, and very often
    they aren't.  PHP supports four scalar value types: <type>int</type>
    values, floating point values (<type>float</type>), <type>string</type>
    values and <type>bool</type> values (scalar values are values that you
    can't 'break' 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.
   </simpara>
   <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've already dealt with, <code>$a = 5</code>.  It's easy to see that
    there are two values involved here, the value of the integer
    constant <code>5</code>, and the value of <varname>$a</varname> which is being updated to 5 as
    well.  But the truth is that there's one additional value involved
    here, and that's the value of the assignment itself.  The
    assignment itself evaluates to the assigned value, in this case 5.
    In practice, it means that <code>$a = 5</code>, regardless of what it does,
    is an expression with the value 5.  Thus, writing something like
    <code>$b = ($a = 5)</code> is like writing
    <code>$a = 5; $b = 5;</code> (a semicolon
    marks the end of a statement).  Since assignments are parsed in a
    right to left order, you can also write <code>$b = $a = 5</code>.
   </simpara>
   <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 <code>variable++</code> and
    <code>variable--</code>.  These are <link linkend="language.operators.increment">
    increment and decrement operators</link>.  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 <code>++$variable</code>, evaluates to the
    incremented value (PHP increments the variable before reading its
    value, thus the name 'pre-increment').  Post-increment, which is
    written <code>$variable++</code> evaluates to the original value of
    <varname>$variable</varname>, before it was incremented (PHP increments the variable
    after reading its value, thus the name 'post-increment').
   </simpara>
   <simpara>
    A very common type of expressions are <link
    linkend="language.operators.comparison">comparison</link>
    expressions. These expressions evaluate to either  &false; or &true;. 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 <code>if</code> statements.
   </simpara>
   <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 <varname>$a</varname> by 1, you can simply write
    <code>$a++</code> or <code>++$a</code>.
    But what if you want to add more than one to it, for instance 3?
    You could write <code>$a++</code> multiple times, but this
    is obviously not a very efficient or comfortable way.  A much more
    common practice is to write <code>$a =
    $a + 3</code>.  <code>$a + 3</code> evaluates
    to the value of <varname>$a</varname> plus 3, and is assigned back
    into <varname>$a</varname>, which results in incrementing <varname>$a</varname>
    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 <varname>$a</varname>
    can be written <code>$a += 3</code>.  This means exactly
    "take the value of <varname>$a</varname>, add 3 to it, and assign it
    back into <varname>$a</varname>". In addition to being shorter and
    clearer, this also results in faster execution.  The value of
    <code>$a += 3</code>, like the value of a regular assignment, is
    the assigned value. Notice that it is NOT 3, but the combined value
    of <varname>$a</varname> plus 3 (this is the value that's
    assigned into <varname>$a</varname>).  Any two-place operator can be used
    in this operator-assignment mode, for example <code>$a -= 5</code>
    (subtract 5 from the value of <varname>$a</varname>), <code>$b *= 7</code>
    (multiply the value of <varname>$b</varname> by 7), etc.
   </simpara>
   <para>
    There is one more expression that may seem odd if you haven't seen
    it in other languages, the ternary conditional operator:
   </para>
   <para>
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
$first ? $second : $third
?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <para>
    If the value of the first subexpression is &true; (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.
   </para>
   <para>
    The following example should help you understand pre- and
    post-increment and expressions in general a bit better:
   </para>
   <para>
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
function double($i)
{
    return $i*2;
}
$b = $a = 5;        /* assign the value five into the variable $a and $b */
$c = $a++;          /* post-increment, assign original value of $a 
                       (5) to $c */
$e = $d = ++$b;     /* pre-increment, assign the incremented value of 
                       $b (6) to $d and $e */

/* at this point, both $d and $e are equal to 6 */

$f = double($d++);  /* assign twice the value of $d before
                       the increment, 2*6 = 12 to $f */
$g = double(++$e);  /* assign twice the value of $e after
                       the increment, 2*7 = 14 to $g */
$h = $g += 10;      /* first, $g is incremented by 10 and ends with the 
                       value of 24. the value of the assignment (24) is 
                       then assigned into $h, and $h ends with the value 
                       of 24 as well. */
?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Some expressions can be considered as statements. In
    this case, a statement has the form of '<code>expr ;</code>' that is, an
    expression followed by a semicolon.  In <code>$b = $a = 5;</code>,
    <code>$a = 5</code> is a valid expression, but it's not a statement
    by itself. <code>$b = $a = 5;</code>, however, is a valid statement.
   </simpara>
   <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 &true; or &false;.
    
    <!-- (PHP doesn't have a
    dedicated boolean type) : WRONG, PHP does. -->
    
    The constants &true; and &false; (case-insensitive) are the two 
    possible boolean values. When necessary, an expression is 
    automatically converted to boolean. See the 
    <link linkend="language.types.typecasting">section about
    type-casting</link> for details about how.
   </simpara>
   <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 <varname>expr</varname>
    to indicate any valid PHP expression.
   </simpara>
  </chapter>
 
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->