language/expressions.xml
4decb44c7141a97e348a1235bbb20d930d2baac0
...
...
@@ -10,15 +10,15 @@
10
10
</simpara>
11
11
<simpara>
12
12
The most basic forms of expressions are constants and variables.
13
-
When you type "<varname>$a</varname> = 5", you're assigning '5' into
14
-
<varname>$a</varname>. '5', obviously,
15
-
has the value 5, or in other words '5' is an expression with the
16
-
value of 5 (in this case, '5' is an integer constant).
13
+
When you type <code>$a = 5</code>, you're assigning <code>5</code> into
14
+
<varname>$a</varname>. <code>5</code>, obviously,
15
+
has the value 5, or in other words <code>5</code> is an expression with the
16
+
value of 5 (in this case, <code>5</code> is an integer constant).
17
17
</simpara>
18
18
<simpara>
19
19
After this assignment, you'd expect <varname>$a</varname>'s value to be 5 as
20
-
well, so if you wrote <varname>$b</varname> = <varname>$a</varname>, you'd expect it to behave just as
21
-
if you wrote <varname>$b</varname> = 5. In other words, <varname>$a</varname> is an expression with the
20
+
well, so if you wrote <code>$b = $a</code>, you'd expect it to behave just as
21
+
if you wrote <code>$b = 5</code>. In other words, <varname>$a</varname> is an expression with the
22
22
value of 5 as well. If everything works right, this is exactly
23
23
what will happen.
24
24
</simpara>
...
...
@@ -42,17 +42,17 @@ function foo ()
42
42
Assuming you're familiar with the concept of functions (if you're
43
43
not, take a look at the chapter about <link
44
44
linkend="language.functions">functions</link>), you'd assume
45
-
that typing <literal>$c = foo()</literal> is essentially just like
46
-
writing <literal>$c = 5</literal>, and you're right. Functions
47
-
are expressions with the value of their return value. Since <literal>foo()</literal>
48
-
returns 5, the value of the expression '<literal>foo()</literal>' is 5. Usually
45
+
that typing <code>$c = foo()</code> is essentially just like
46
+
writing <code>$c = 5</code>, and you're right. Functions
47
+
are expressions with the value of their return value. Since <code>foo()</code>
48
+
returns 5, the value of the expression '<code>foo()</code>' is 5. Usually
49
49
functions don't just return a static value but compute something.
50
50
</simpara>
51
51
<simpara>
52
52
Of course, values in PHP don't have to be integers, and very often
53
-
they aren't. PHP supports four scalar value types: <type>integer</type>
53
+
they aren't. PHP supports four scalar value types: <type>int</type>
54
54
values, floating point values (<type>float</type>), <type>string</type>
55
-
values and <type>boolean</type> values (scalar values are values that you
55
+
values and <type>bool</type> values (scalar values are values that you
56
56
can't 'break' into smaller pieces, unlike arrays, for instance). PHP also
57
57
supports two composite (non-scalar) types: arrays and objects. Each of
58
58
these value types can be assigned into variables or returned from functions.
...
...
@@ -61,34 +61,34 @@ function foo ()
61
61
PHP takes expressions much further, in the same way many other languages
62
62
do. PHP is an expression-oriented language, in the
63
63
sense that almost everything is an expression. Consider the
64
-
example we've already dealt with, '<varname>$a</varname> = 5'. It's easy to see that
64
+
example we've already dealt with, <code>$a = 5</code>. It's easy to see that
65
65
there are two values involved here, the value of the integer
66
-
constant '5', and the value of <varname>$a</varname> which is being updated to 5 as
66
+
constant <code>5</code>, and the value of <varname>$a</varname> which is being updated to 5 as
67
67
well. But the truth is that there's one additional value involved
68
68
here, and that's the value of the assignment itself. The
69
69
assignment itself evaluates to the assigned value, in this case 5.
70
-
In practice, it means that '<varname>$a</varname> = 5', regardless of what it does,
70
+
In practice, it means that <code>$a = 5</code>, regardless of what it does,
71
71
is an expression with the value 5. Thus, writing something like
72
-
'<varname>$b</varname> = (<varname>$a</varname> = 5)' is like writing
73
-
'<varname>$a</varname> = 5; <varname>$b</varname> = 5;' (a semicolon
72
+
<code>$b = ($a = 5)</code> is like writing
73
+
<code>$a = 5; $b = 5;</code> (a semicolon
74
74
marks the end of a statement). Since assignments are parsed in a
75
-
right to left order, you can also write '<varname>$b</varname> = <varname>$a</varname> = 5'.
75
+
right to left order, you can also write <code>$b = $a = 5</code>.
76
76
</simpara>
77
77
<simpara>
78
78
Another good example of expression orientation is pre- and
79
79
post-increment and decrement. Users of PHP and many other
80
-
languages may be familiar with the notation of <literal>variable++</literal> and
81
-
<literal>variable--</literal>. These are <link linkend="language.operators.increment">
80
+
languages may be familiar with the notation of <code>variable++</code> and
81
+
<code>variable--</code>. These are <link linkend="language.operators.increment">
82
82
increment and decrement operators</link>. In PHP, like in C, there
83
83
are two types of increment - pre-increment and post-increment.
84
84
Both pre-increment and post-increment essentially increment the
85
85
variable, and the effect on the variable is identical. The
86
86
difference is with the value of the increment expression.
87
-
Pre-increment, which is written '++<varname>$variable</varname>', evaluates to the
87
+
Pre-increment, which is written <code>++$variable</code>, evaluates to the
88
88
incremented value (PHP increments the variable before reading its
89
89
value, thus the name 'pre-increment'). Post-increment, which is
90
-
written '<varname>$variable</varname>++' evaluates to the original value of
91
-
$variable, before it was incremented (PHP increments the variable
90
+
written <code>$variable++</code> evaluates to the original value of
91
+
<varname>$variable</varname>, before it was incremented (PHP increments the variable
92
92
after reading its value, thus the name 'post-increment').
93
93
</simpara>
94
94
<simpara>
...
...
@@ -100,33 +100,33 @@ function foo ()
100
100
The language also supports a set of strict equivalence operators: ===
101
101
(equal to and same type) and !== (not equal to or not same type).
102
102
These expressions are most commonly used inside conditional execution,
103
-
such as <literal>if</literal> statements.
103
+
such as <code>if</code> statements.
104
104
</simpara>
105
105
<simpara>
106
106
The last example of expressions we'll deal with here is combined
107
107
operator-assignment expressions. You already know that if you
108
108
want to increment <varname>$a</varname> by 1, you can simply write
109
-
'<varname>$a</varname>++' or '++<varname>$a</varname>'.
109
+
<code>$a++</code> or <code>++$a</code>.
110
110
But what if you want to add more than one to it, for instance 3?
111
-
You could write '<varname>$a</varname>++' multiple times, but this
111
+
You could write <code>$a++</code> multiple times, but this
112
112
is obviously not a very efficient or comfortable way. A much more
113
-
common practice is to write '<varname>$a</varname> =
114
-
<varname>$a</varname> + 3'. '<varname>$a</varname> + 3' evaluates
113
+
common practice is to write <code>$a =
114
+
$a + 3</code>. <code>$a + 3</code> evaluates
115
115
to the value of <varname>$a</varname> plus 3, and is assigned back
116
116
into <varname>$a</varname>, which results in incrementing <varname>$a</varname>
117
117
by 3. In PHP, as in several other languages like C, you can write this
118
118
in a shorter way, which with time would become clearer and quicker to
119
119
understand as well. Adding 3 to the current value of <varname>$a</varname>
120
-
can be written '<varname>$a</varname> += 3'. This means exactly
120
+
can be written <code>$a += 3</code>. This means exactly
121
121
"take the value of <varname>$a</varname>, add 3 to it, and assign it
122
122
back into <varname>$a</varname>". In addition to being shorter and
123
123
clearer, this also results in faster execution. The value of
124
-
'<varname>$a</varname> += 3', like the value of a regular assignment, is
124
+
<code>$a += 3</code>, like the value of a regular assignment, is
125
125
the assigned value. Notice that it is NOT 3, but the combined value
126
126
of <varname>$a</varname> plus 3 (this is the value that's
127
127
assigned into <varname>$a</varname>). Any two-place operator can be used
128
-
in this operator-assignment mode, for example '<varname>$a</varname> -= 5'
129
-
(subtract 5 from the value of <varname>$a</varname>), '<varname>$b</varname> *= 7'
128
+
in this operator-assignment mode, for example <code>$a -= 5</code>
129
+
(subtract 5 from the value of <varname>$a</varname>), <code>$b *= 7</code>
130
130
(multiply the value of <varname>$b</varname> by 7), etc.
131
131
</simpara>
132
132
<para>
...
...
@@ -186,10 +186,10 @@ $h = $g += 10; /* first, $g is incremented by 10 and ends with the
186
186
</para>
187
187
<simpara>
188
188
Some expressions can be considered as statements. In
189
-
this case, a statement has the form of '<literal>expr ;</literal>' that is, an
190
-
expression followed by a semicolon. In <literal>'$b = $a = 5;'</literal>,
191
-
<literal>'$a = 5'</literal> is a valid expression, but it's not a statement
192
-
by itself. <literal>'$b = $a = 5;'</literal> however is a valid statement.
189
+
this case, a statement has the form of '<code>expr ;</code>' that is, an
190
+
expression followed by a semicolon. In <code>$b = $a = 5;</code>,
191
+
<code>$a = 5</code> is a valid expression, but it's not a statement
192
+
by itself. <code>$b = $a = 5;</code>, however, is a valid statement.
193
193
</simpara>
194
194
<simpara>
195
195
One last thing worth mentioning is the truth value of expressions.
196
196