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:
<?php
function foo ()
{
return 5;
}
?>
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 <= (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 5
4 statements.
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 5
5 or 5
6. But what if you want to add more than one to it, for instance 3? You could write 5
5 multiple times, but this is obviously not a very efficient or comfortable way. A much more
common practice is to write 5
8. 5
9 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 5
0. 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 5
0, 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 5
2 (subtract 5 from the value of $a), 5
3 (multiply the value of $b by
7), etc.
Có một biểu thức nữa có vẻ kỳ lạ nếu bạn chưa thấy nó trong các ngôn ngữ khác, toán tử điều kiện ternary:
5
4
Nếu giá trị của biểu hiện phụ thứ nhất là 5
3 (không phải), thì biểu hiện phụ thứ hai được đánh giá và đó là kết quả của biểu thức có điều kiện. Mặt khác, biểu hiện phụ thứ ba được đánh giá và đó là giá trị.5
3 (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.
Ví dụ sau đây sẽ giúp bạn hiểu trước và sau khi nhận thức và biểu thức nói chung tốt hơn một chút:
5
6
5
7
5
8 Một số biểu thức có thể được coi là tuyên bố. Trong trường hợp này, một tuyên bố có dạng '5
9' nghĩa là, một biểu thức theo sau là dấu chấm phẩy. Trong 5
0, $a = 5
là một biểu thức hợp lệ, nhưng nó không phải là một tuyên bố. 5
0, tuy nhiên, là một tuyên bố hợp lệ.
Một điều cuối cùng đáng nói là giá trị sự thật của các biểu hiện. Trong nhiều sự kiện, chủ yếu trong việc thực thi và vòng lặp có điều kiện, bạn không quan tâm đến giá trị cụ thể của biểu thức, nhưng chỉ quan tâm đến việc nó có nghĩa là 5
3 hay 5
2. Các hằng số 5
3 và 5
2 (không nhạy cảm trường hợp) là hai giá trị boolean có thể. Khi cần thiết, một biểu thức được tự động chuyển đổi thành boolean. Xem phần về cách đúc loại để biết chi tiết về cách thức.5
3 or 5
2. The constants 5
3 and 5
2 (case-insensitive) are the two possible boolean values. When necessary, an expression is automatically converted to boolean. See the section about
type-casting for details about how.
PHP cung cấp một triển khai đầy đủ và mạnh mẽ của các biểu thức và ghi lại nó hoàn toàn vượt ra ngoài phạm vi của hướng dẫn này. Các ví dụ trên sẽ cho bạn một ý tưởng tốt về các biểu thức là gì và làm thế nào bạn có thể xây dựng các biểu thức hữu ích. Trong suốt phần còn lại của hướng dẫn này, chúng tôi sẽ viết expr để chỉ ra bất kỳ biểu thức PHP hợp lệ nào.
Magnus Deininger, DMA05 tại Web Dot de ¶ ¶
13 năm trước
5
7
5
8
5
9
$b = $a
0
$b = $a
1 yasuo_ohgaki tại hotmail dot com ¶
21 năm trước
$b = $a
2
$b = $a
3
$b = $a
4
$b = $a
5
$b = $a
6
$b = $a
7
$b = $a
8
$b = $a
9
$b = 5
0 Mattias tại mail dot ee ¶ ¶
20 năm trước
$b = 5
1
$b = 5
2
$b = 5
3
$b = 5
4
$b = 5
5
$b = 5
6
$b = 5
0 Chriswarbo tại Gmail Dot Com ¶ ¶
9 năm trước
$b = 5
8
$b = 5
9
<?php
function foo ()
{
return 5;
}
?>
0
<?php
function foo ()
{
return 5;
}
?>
1
<?php
function foo ()
{
return 5;
}
?>
2
<?php
function foo ()
{
return 5;
}
?>
3
<?php
function foo ()
{
return 5;
}
?>
4
<?php
function foo ()
{
return 5;
}
?>
5
<?php
function foo ()
{
return 5;
}
?>
6
<?php
function foo ()
{
return 5;
}
?>
7
<?php
function foo ()
{
return 5;
}
?>
8
$b = 5
0
Oliver 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
0 Chriswarbo 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
0 Oliver tại Hankeln-Online Dot de ¶ ¶
9 năm trước
$c = 5
4
$c = 5
5
$b = 5
0 Oliver 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
0 Oliver 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
0 Oliver tại Hankeln-Online Dot de ¶ ¶
nháy mắt716 ¶
5
12
5
13
5
14
5
15
5
16
$b = 5
0 15 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
0 Mattias 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
0 15 năm trước ¶
nháy mắt716 ¶
5
35
5
36
5
37
5
38
5
39
5
40
$b = 5
0 15 năm trước ¶
nháy mắt716 ¶
5
42
5
43
5
44 15 năm trước ¶
9 năm trước
5
45
5
46
5
47
$b = 5
0
Oliver tại Hankeln-Online Dot de ¶ ¶
9 năm trước
5
49
5
50
$b = 5
0 Oliver tại Hankeln-Online Dot de ¶ ¶
nháy mắt716 ¶
5
52
5
53
5
54
5
55
5
56
5
57
$b = 5
0 15 năm trước ¶
nháy mắt716 ¶
5
59
5
60
5
61
5
62
5
63
$b = 5
0Trong PHP, các biểu thức thông thường là các chuỗi bao gồm các dấu phân cách, một mẫu và các biến đổi tùy chọn. $ exp = "/w3schools/i"; Trong ví dụ trên, / là dấu phân cách, W3Schools là mẫu đang được tìm kiếm và tôi là một công cụ sửa đổi làm cho trường hợp tìm kiếm không nhạy cảm.strings composed of delimiters, a pattern and optional modifiers. $exp = "/w3schools/i"; In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.
Hàm endswith () được sử dụng để kiểm tra xem một chuỗi kết thúc bằng chuỗi đã cho hay không.Hàm này là trường hợp không nhạy cảm và nó trả về giá trị boolean.Hàm endswith () có thể được sử dụng với hàm bộ lọc để tìm kiếm dữ liệu.endsWith() function is used to test whether a string ends with the given string or not. This function is case insensitive and it returns boolean value. The endsWith() function can be used with the Filter function to search the data.