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".
The most basic forms of expressions are constants and variables. When you type $a = 5
, you're assigning 5
into $a. 5
, obviously, has the value 5, or in other words 5
is an expression with the value of
5 [in this case, 5
is an integer constant].
After this assignment, you'd expect $a's value to be 5 as well, so if you wrote $b = $a
, you'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.
Slightly more complex examples for expressions are functions. For instance, consider the following function:
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 $c = foo[]
is essentially just like writing $c = 5
, and you're right. Functions are expressions with the value of their return value. Since 5
0 returns 5, the value of the expression '5
0' is 5. Usually functions don't just return a static value but
compute something.
Of course, values in PHP don't have to be integers, and very often they aren't. PHP supports four scalar value types: int values, floating point values [float], string values and bool 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.
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 5
5 is like writing 5
6 [a semicolon marks the end of a statement]. Since assignments are parsed in a right to left order, you can also write 5
7.
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 5
8 and 5
9. These are increment and decrement operators. 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 5
0, evaluates to the incremented value [PHP increments the variable before reading its value, thus the name 'pre-increment']. Post-increment, which is written 5
1 evaluates to the original value of $variable, before it was incremented [PHP increments the variable after reading its value, thus the name 'post-increment'].
A very common type of expressions are
comparison expressions. These expressions evaluate to either 5
2 or 5
3. PHP supports > [bigger than], >= [bigger than or equal to], == [equal], != [not equal], < [smaller than] and
0
1
2
3
4
5
6
7
8
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
20 năm trước
$c = foo[]
0
$c = foo[]
1
$c = foo[]
2
$c = foo[]
3
$b = 5
0Chriswarbo tại Gmail Dot Com ¶ ¶
9 năm trước
$c = foo[]
5
$c = foo[]
6
$c = foo[]
7
$c = foo[]
8
$c = foo[]
9
$c = 5
0
$c = 5
1
$c = 5
2
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
9 năm trước
$c = 5
4
$c = 5
5
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
9 năm trước
$c = 5
7
$c = 5
8
$c = 5
9
5
00
5
01
$c = foo[]
7
$c = foo[]
8
5
04
5
05
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
nháy mắt716 ¶
5
07
15 năm trước ¶
9 năm trước
5
08
5
09
5
10
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
nháy mắt716 ¶
5
12
5
13
5
14
5
15
5
16
$b = 5
015 năm trước ¶
21 năm trước
5
18
5
19
5
20
5
21
5
22
5
23
5
24
5
25
$b = 5
0Mattias tại mail dot ee ¶ ¶
20 năm trước
5
27
Chriswarbo tại Gmail Dot Com ¶ ¶
9 năm trước
5
28
5
29
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
nháy mắt716 ¶
5
31
5
32
5
33
$b = 5
015 năm trước ¶
nháy mắt716 ¶
5
35
5
36
5
37
5
38
5
39
5
40
$b = 5
015 năm trước ¶
nháy mắt716 ¶
5
42
5
43
5
4415 năm trước ¶
9 năm trước
5
45
5
46
5
47
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
9 năm trước
5
49
5
50
$b = 5
0Oliver tại Hankeln-Online Dot de ¶ ¶
nháy mắt716 ¶
5
52
5
53
5
54
5
55
5
56
5
57
$b = 5
015 năm trước ¶
nháy mắt716 ¶
5
59
5
60
5
61
5
62
5
63
$b = 5
0