Hướng dẫn what are the expressions and statements in php? - các biểu thức và câu lệnh trong php là gì?

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:

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 50 returns 5, the value of the expression '50' 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 55 is like writing 56 (a semicolon marks the end of a statement). Since assignments are parsed in a right to left order, you can also write 57.

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 58 and 59. 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 50, evaluates to the incremented value (PHP increments the variable before reading its value, thus the name 'pre-increment'). Post-increment, which is written 51 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 52 or 53. 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 54 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 55 or 56. But what if you want to add more than one to it, for instance 3? You could write 55 multiple times, but this is obviously not a very efficient or comfortable way. A much more common practice is to write 58. 59 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 50. 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 50, 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 52 (subtract 5 from the value of $a), 53 (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:

54

Nếu giá trị của biểu hiện phụ thứ nhất là 53 (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ị.53 (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:

56

57

58

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 '59' nghĩa là, một biểu thức theo sau là dấu chấm phẩy. Trong 50, $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ố. 50, 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à 53 hay 52. Các hằng số 53 và 52 (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.53 or 52. The constants 53 and 52 (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

57

58

59

$b = $a0

$b = $a1

yasuo_ohgaki tại hotmail dot com

21 năm trước

$b = $a2

$b = $a3

$b = $a4

$b = $a5

$b = $a6

$b = $a7

$b = $a8

$b = $a9

$b = 50

Mattias tại mail dot ee ¶

20 năm trước

$b = 51

$b = 52

$b = 53

$b = 54

$b = 55

$b = 56

$b = 50

Chriswarbo tại Gmail Dot Com ¶

9 năm trước

$b = 58

$b = 59

function foo ()
{
    return 
5;
}
?>
0

function foo ()
{
    return 
5;
}
?>
1

function foo ()
{
    return 
5;
}
?>
2

function foo ()
{
    return 
5;
}
?>
3

function foo ()
{
    return 
5;
}
?>
4

function foo ()
{
    return 
5;
}
?>
5

function foo ()
{
    return 
5;
}
?>
6

function foo ()
{
    return 
5;
}
?>
7

function foo ()
{
    return 
5;
}
?>
8

$b = 50

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 = 50

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 = 50

$c = 51

$c = 52

$b = 50

Oliver tại Hankeln-Online Dot de ¶

9 năm trước

$c = 54

$c = 55

$b = 50

Oliver tại Hankeln-Online Dot de ¶

9 năm trước

$c = 57

$c = 58

$c = 59

500

501

$c = foo()7

$c = foo()8

504

505

$b = 50

Oliver tại Hankeln-Online Dot de ¶

nháy mắt716 ¶

507

15 năm trước

9 năm trước

508

509

510

$b = 50

Oliver tại Hankeln-Online Dot de ¶

nháy mắt716 ¶

512

513

514

515

516

$b = 50

15 năm trước

21 năm trước

518

519

520

521

522

523

524

525

$b = 50

Mattias tại mail dot ee ¶

20 năm trước

527

Chriswarbo tại Gmail Dot Com ¶

9 năm trước

528

529

$b = 50

Oliver tại Hankeln-Online Dot de ¶

nháy mắt716 ¶

531

532

533

$b = 50

15 năm trước

nháy mắt716 ¶

535

536

537

538

539

540

$b = 50

15 năm trước

nháy mắt716 ¶

542

543

544

15 năm trước

9 năm trước

545

546

547

$b = 50

Oliver tại Hankeln-Online Dot de ¶

9 năm trước

549

550

$b = 50

Oliver tại Hankeln-Online Dot de ¶

nháy mắt716 ¶

552

553

554

555

556

557

$b = 50

15 năm trước

nháy mắt716 ¶

559

560

561

562

563

$b = 50

Các biểu thức trong PHP là gì?

Trong 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.

Sự khác biệt giữa tuyên bố và biểu thức trong PHP là gì?

Trong thuật ngữ ngôn ngữ lập trình, một biểu thức trên mạng là sự kết hợp giữa các giá trị và hàm được trình biên dịch kết hợp và giải thích để tạo ra một giá trị mới, trái ngược với một câu lệnhbất cứ điều gì.

Hình thức biểu thức đơn giản nhất trong PHP là gì?

Trong PHP, hầu hết mọi thứ bạn viết là một biểu thức.Cách đơn giản nhất nhưng chính xác nhất để xác định biểu thức là "bất cứ điều gì có giá trị".Các dạng biểu thức cơ bản nhất là hằng số và biến.anything that has a value". The most basic forms of expressions are constants and variables.

Các biểu thức và luồng kiểm soát trong PHP là gì?

Biểu thức và luồng điều khiển trong PHP - Học PHP, MySQL, JavaScript, CSS & HTML5, Phiên bản thứ 3 [Sách] ... Các toán tử logic ..