Hướng dẫn control structures in php geeksforgeeks - cấu trúc điều khiển trong php geeksforgeeks

Cải thiện bài viết

Lưu bài viết

PHP cho phép chúng tôi thực hiện các hành động dựa trên một số loại điều kiện có thể là logic hoặc so sánh. Dựa trên kết quả của các điều kiện này, tức là đúng hoặc sai, một hành động sẽ được thực hiện theo yêu cầu của người dùng. Nó giống như một con đường hai chiều. Nếu bạn muốn một cái gì đó thì hãy đi theo cách này hoặc cách khác xoay theo cách đó. Để sử dụng tính năng này, PHP cung cấp cho chúng tôi bốn câu lệnh có điều kiện:

  • Nếu tuyên bố statement
  • Nếu tuyên bố khác statement
  • Nếu khác thì khác thì tuyên bố khác statement
  • Tuyên bố chuyển đổi statement

Bây giờ chúng ta hãy nhìn vào từng trong số này chi tiết:

  1. Nếu tuyên bố: Tuyên bố này cho phép chúng tôi đặt một điều kiện. Khi đúng, khối mã sau được đặt trong mệnh đề IF sẽ được thực thi.: This statement allows us to set a condition. On being TRUE, the following block of code enclosed within the if clause will be executed.

    Cú pháp::

    if (condition){
        // if TRUE then execute this code
    }
    

    Example:

    The number is positive
    
    0

    The number is positive
    
    1
    The number is positive
    
    2

    The number is positive
    
    3
    The number is positive
    
    4
    The number is positive
    
    1
    The number is positive
    
    6

    The number is positive
    
    7
    The number is positive
    
    8
    The number is positive
    
    9
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    1

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    2

    Output:

    The number is positive
    

    Flowchart::

    Hướng dẫn control structures in php geeksforgeeks - cấu trúc điều khiển trong php geeksforgeeks

  2. Nếu tuyên bố khác: Chúng tôi hiểu rằng nếu một điều kiện sẽ giữ, thì đúng, thì khối mã bên trong nếu sẽ được thực thi. Nhưng điều gì sẽ xảy ra nếu điều kiện không đúng và chúng ta muốn thực hiện một hành động? Đây là nơi khác đi vào chơi. Nếu một điều kiện là đúng thì nếu khối được thực thi, nếu không khối sẽ được thực thi.: We understood that if a condition will hold i.e., TRUE, then the block of code within if will be executed. But what if the condition is not TRUE and we want to perform an action? This is where else comes into play. If a condition is TRUE then if block gets executed, otherwise else block gets executed.

    Syntax::

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    

    Example:

    The number is positive
    
    0

    The number is positive
    
    1
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    5

    The number is positive
    
    3
    The number is positive
    
    4
    The number is positive
    
    1
    The number is positive
    
    6

    The number is positive
    
    7
    The number is positive
    
    8
    The number is positive
    
    9
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    1

    The number is negative
    
    5
    The number is negative
    
    6

    Nếu tuyên bố khác: Chúng tôi hiểu rằng nếu một điều kiện sẽ giữ, thì đúng, thì khối mã bên trong nếu sẽ được thực thi. Nhưng điều gì sẽ xảy ra nếu điều kiện không đúng và chúng ta muốn thực hiện một hành động? Đây là nơi khác đi vào chơi. Nếu một điều kiện là đúng thì nếu khối được thực thi, nếu không khối sẽ được thực thi.

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    1

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    2

    Output:

    The number is positive
    
    1
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    5

    Flowchart::

    Hướng dẫn control structures in php geeksforgeeks - cấu trúc điều khiển trong php geeksforgeeks

  3. The number is positive
    
    7
    The number is positive
    
    8
    The number is negative
    
    9
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0
    : This allows us to use multiple if…else statements. We use this when there are multiple conditions of TRUE cases.
    Syntax:
    if (condition) {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    else {
        // if FALSE then execute this code
    }
    

    Example:

    The number is positive
    
    0

    The number is negative
    

    Nếu câu nói khác của người khác: Điều này cho phép chúng tôi sử dụng nhiều câu lệnh khác. Chúng tôi sử dụng điều này khi có nhiều điều kiện của các trường hợp thực.

    The number is positive
    
    1
    if (condition) {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    else {
        // if FALSE then execute this code
    }
    
    5
    if (condition) {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    else {
        // if FALSE then execute this code
    }
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    1

    The number is positive
    
    3
    The number is positive
    
    4
    The number is positive
    
    1
    Happy Independence Day!!!
    
    1
    Happy Independence Day!!!
    
    22

    The number is positive
    
    7
    The number is positive
    
    8
    Happy Independence Day!!!
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    1

    The number is negative
    
    5
    The number is negative
    
    6

    Happy Independence Day!!!
    
    9
    The number is positive
    
    4
    The number is positive
    
    1
    Happy Independence Day!!!
    
    1
    if (condition) {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    else {
        // if FALSE then execute this code
    }
    
    6
    Happy Independence Day!!!
    
    3

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    1

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    2

    Output:

    Happy Independence Day!!!
    

    Flowchart::

    Hướng dẫn control structures in php geeksforgeeks - cấu trúc điều khiển trong php geeksforgeeks

  4. The number is positive
    
    7
    The number is positive
    
    8
    switch(n) {
        case statement1:
            code to be executed if n==statement1;
            break;
        case statement2:
            code to be executed if n==statement2;
            break;
        case statement3:
            code to be executed if n==statement3;
            break;
        case statement4:
            code to be executed if n==statement4;
            break;
        ......
        default:
            code to be executed if n != any case;
    
    
    7
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0
    : The “switch” performs in various cases i.e., it has various cases to which it matches the condition and appropriately executes a particular case block. It first evaluates an expression and then compares with the values of each case. If a case matches then the same case is executed. To use switch, we need to get familiar with two different keywords namely, break and default.
    1. The number is positive
      
      7
      The number is positive
      
      8
      Its February
      
      4
      if (condition) {
          // if TRUE then execute this code
      }
      else{
          // if FALSE then execute this code
      }
      
      0break statement is used to stop the automatic control flow into the next cases and exit from the switch case.
    2. Tuyên bố chuyển đổi: Công tắc trực tuyến thực hiện trong các trường hợp khác nhau, tức là, nó có nhiều trường hợp khác nhau phù hợp với điều kiện và thực hiện một cách thích hợp một khối trường hợp cụ thể. Trước tiên, nó đánh giá một biểu thức và sau đó so sánh với các giá trị của từng trường hợp. Nếu một trường hợp khớp với cùng một trường hợp được thực thi. Để sử dụng Switch, chúng ta cần làm quen với hai từ khóa khác nhau là phá vỡ và mặc định.default statement contains the code that would execute if none of the cases match.

    Syntax::

    switch(n) {
        case statement1:
            code to be executed if n==statement1;
            break;
        case statement2:
            code to be executed if n==statement2;
            break;
        case statement3:
            code to be executed if n==statement3;
            break;
        case statement4:
            code to be executed if n==statement4;
            break;
        ......
        default:
            code to be executed if n != any case;
    
    

    Example:

    The number is positive
    
    0

    Câu lệnh Break được sử dụng để dừng luồng điều khiển tự động vào các trường hợp tiếp theo và thoát khỏi trường hợp chuyển đổi.

    (condition) ? if TRUE execute this : otherwise execute this;
    3
    The number is positive
    
    4
    Its February
    
    9
    Happy Independence Day!!!
    
    3

    Câu lệnh mặc định chứa mã sẽ thực thi nếu không có trường hợp nào khớp.

    Its February
    
    9
    if (condition) {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    else {
        // if FALSE then execute this code
    }
    
    5
    (condition) ? if TRUE execute this : otherwise execute this;
    1
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    Happy Independence Day!!!
    
    22
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is negative
    The number is negative
    
    3
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    (condition) ? if TRUE execute this : otherwise execute this;
    1
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    04
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    11
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    15
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    222
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    26
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    33
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    37
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    44
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    48
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    55
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    59
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    if (condition) {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    elseif {
        // if TRUE then execute this code
    }
    else {
        // if FALSE then execute this code
    }
    
    6
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    70
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    777____90

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    81
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    88
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    The number is positive
    
    92
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    The number is positive
    
    99
    The number is negative
    The number is negative
    
    0

    The number is negative
    The number is negative
    
    1
    The number is positive
    
    8
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    03
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is negative
    The number is negative
    
    1
    The number is negative
    The number is negative
    
    6
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    0

    The number is positive
    
    7
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    20
    The number is negative
    The number is negative
    
    0

    The number is positive
    
    7
    (condition) ? if TRUE execute this : otherwise execute this;
    8
    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    10
    The number is negative
    The number is negative
    
    0

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    1

    if (condition) {
        // if TRUE then execute this code
    }
    else{
        // if FALSE then execute this code
    }
    
    2

    Output:

    Its February
    

    Flowchart::

    Hướng dẫn control structures in php geeksforgeeks - cấu trúc điều khiển trong php geeksforgeeks

The number is negative
The number is negative
1
The number is positive
8
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
14
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
0

The number is negative
The number is negative
1
The number is positive
8
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
24
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
0
Syntax:

(condition) ? if TRUE execute this : otherwise execute this;

Example:

The number is positive
0

The number is positive
1
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
5

The number is positive
3
The number is positive
4
The number is positive
1
The number is positive
6

The number is positive
7
The number is positive
8
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
37
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
0

if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
1

The number is negative
5
The number is negative
6

The number is positive
7
The number is positive
8
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
44
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
0

if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
1

The number is positive
8
The number is positive
4
The number is positive
1
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
50
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
51
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
52

if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
53
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
54
if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
0

if (condition) {
    // if TRUE then execute this code
}
else{
    // if FALSE then execute this code
}
2

Output:

The number is negative
The number is negative

Bài viết này được đóng góp bởi Chinmoy Lenka. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.


Cấu trúc kiểm soát trong PHP là gì?

Trong PHP, có hai loại cấu trúc kiểm soát chính: báo cáo có điều kiện và vòng điều khiển.Conditional Statements and Control Loops.

Các câu lệnh kiểm soát trong PHP là gì?

Tuyên bố kiểm soát khác nhau trong PHP..
if.else..
if.else..if..
Tuyên bố chuyển đổi ..

Các loại cấu trúc kiểm soát quyết định trong ngôn ngữ PHP xác định từng loại với các ví dụ là gì?

IF, otherif ... khác và các câu lệnh chuyển đổi được sử dụng để đưa ra quyết định dựa trên điều kiện khác nhau.Câu lệnh SWITEN - được sử dụng nếu bạn muốn chọn một trong nhiều khối mã sẽ được thực thi, hãy sử dụng câu lệnh Switch.Câu lệnh Switch được sử dụng để tránh các khối dài nếu .. otherif..else mã.if, elseif ...else and switch statements are used to take decision based on the different condition. switch statement − is used if you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if.. elseif..else code.

Cấu trúc và vòng lặp có điều kiện được sử dụng trong PHP là gì?

Trong PHP, chúng tôi có các câu lệnh có điều kiện sau: nếu câu lệnh - thực thi một số mã nếu một điều kiện là đúng.Nếu ... câu lệnh khác - thực thi một số mã nếu một điều kiện là đúng và mã khác nếu điều kiện đó là sai.Nếu ... otherif ... câu lệnh khác - thực thi các mã khác nhau cho nhiều hơn hai điều kiện.