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
|
<?xml version="1.0" encoding="utf-8"?>
<chapter id="language.expressions">
<title>표현식 (Expressions)</title>
<simpara>
표현식은 PHP에서 매우 중요한 내용이다.
PHP에서 여러분이 작성하는 거의 모든 것은 표현식이다.
간단히 표현식의 정의를 엄밀하게 말한다면 "값을 가지고 있는 모든 것"("anything that has a value")이다.
</simpara>
<simpara>
간단한 예제로 "$a = 5"와 같은 것이 있다. 이것은 '5'라는 값을 $a라는 변수에 대입하는 문장이다.
여기서 '5'라는 표현식은 값 5 라는 의미가 된다.
</simpara>
<simpara>
위의 문장 다음에 "$b = $a"라는 문장이 있다면, 여기서 $a라는 표현식은 그 변수가 가진 5라는 값을 표현한다.
</simpara>
<para>
조금 복잡한 경우는 함수이다. 다음 함수를 보자. :
<informalexample>
<programlisting>
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 functions), you'd assume
that typing <literal>$c = foo()</literal> is essentially just like
writing <literal>$c = 5</literal>, and you're right. Functions
are expressions with the value of their return value. Since foo()
returns 5, the value of the expression 'foo()' 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 three scalar value types: integer values,
floating point values and string 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>
So far, users of PHP/FI 2 shouldn't feel any change. However, 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, '$a = 5'. It's easy to see that
there are two values involved here, the value of the integer
constant '5', and the value of $a 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 '$a = 5', regardless of what it does,
is an expression with the value 5. Thus, writing something like
'$b = ($a = 5)' is like writing '$a = 5; $b = 5;' (a semicolon
marks the end of a statement). Since assignments are parsed in a
right to left order, you can also write '$b = $a = 5'.</simpara>
<simpara>
Another good example of expression orientation is pre- and
post-increment and decrement. Users of PHP/FI 2 and many other
languages may be familiar with the notation of variable++ and
variable--. These are increment and decrement operators. In
PHP/FI 2, the statement '$a++' has no value (is not an
expression), and thus you can'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 idential. The
difference is with the value of the increment expression.
Pre-increment, which is written '++$variable', evaluates to the
incremented value (PHP increments the variable before reading its
value, thus the name 'pre-increment'). Post-increment, which is
written '$variable++' evaluates to the original value of
$variable, 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 comparison expressions.
These expressions evaluate to either 0 or 1, meaning &false; or &true;
(respectively). PHP supports > (bigger than), >= (bigger than
or equal to), == (equal), != (not equal), < (smaller than) and <=
(smaller than or equal to). These expressions are most commonly used
inside conditional execution, such as <literal>if</literal>
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 $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.</simpara>
<para>
There is one more expression that may seem odd if you haven't seen
it in other languages, the ternary conditional operator:
<informalexample>
<programlisting>
$first ? $second : $third
</programlisting>
</informalexample>
If the value of the first subexpression is &true; (non-zero), then
it 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:
<informalexample>
<programlisting>
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 <emphasis>before</emphasis>
the increment, 2*6 = 12 to $f */
$g = double(++$e); /* assign twice the value of $e <emphasis>after</emphasis>
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>
In the beginning of the chapter we said that we'll be describing
the various statement types, and as promised, expressions can be
statements. However, not every expression is a statement. 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.</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). The truth value of expressions in PHP is
calculated in a similar way to perl. Any numeric non-zero numeric
value is &true;, zero is &false;. Be sure to note that negative
values are non-zero and are thus considered &true;! The empty
string and the string "0" are &false;; all other strings are &true;.
With non-scalar values (arrays and objects) - if the value
contains no elements it's considered &false;, otherwise it's
considered &true;.</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 <replaceable>expr</replaceable>
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:"../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->
|