Hướng dẫn control statements in php geeksforgeeks - câu lệnh điều khiển trong php geeksforgeeks

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc

    • Bàn luận statement
    • 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: statement
    • 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

    1. Tuyên bố chuyển đổi: This statement allows us to set a condition. On being TRUE, the following block of code enclosed within the if clause will be executed.

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

      if [condition]{
          // if TRUE then execute this code
      }
      

      Example:

      The number is positive
      
      0

      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.

      Cú pháp:

      The number is positive
      
      1
      The number is positive
      
      2

      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::

    2. The number is positive
      
      3
      The number is positive
      
      4
      The number is positive
      
      1
      The number is positive
      
      6
      : 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
      }
      

      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

      The number is positive
      
      0

      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.

      Cú pháp:

      The number is positive
      
      1
      The number is positive
      
      2

      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
      
      3
      The number is positive
      
      4
      The number is positive
      
      1
      The number is positive
      
      6

      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 negative
      

      Flowchart::

    3. 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
      : 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

      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.

      Thí dụ:

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

      if [condition] {
          // if TRUE then execute this code
      }
      else{
          // if FALSE then execute this code
      }
      
      1

      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

      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.

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

      if [condition] {
          // if TRUE then execute this code
      }
      else{
          // if FALSE then execute this code
      }
      
      2

      Output:

      Happy Independence Day!!!
      

      Flowchart::

    4. The number is positive
      
      3
      The number is positive
      
      4
      The number is positive
      
      1
      Happy Independence Day!!!
      
      1
      Happy Independence Day!!!
      
      22
      : 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
        Happy Independence Day!!!
        
        6
        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. 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!!!
        
        3default statement contains the code that would execute if none of the cases match.

      Syntax::

      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

      Example:

      The number is positive
      
      0

      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
      }
      
      0

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

      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.

      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.

      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

      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.

      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;
      
      

      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

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

      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::

    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 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
    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ác câu lệnh kiểm soát trong PHP là gì?

    Các câu lệnh kiểm soát là các câu lệnh có điều kiện thực thi một khối các câu lệnh nếu điều kiện là chính xác.Câu lệnh bên trong khối có điều kiện sẽ không thực thi cho đến khi điều kiện được thỏa mãn.Câu lệnh IF: IF [Biểu thức1]..

    Bài Viết Liên Quan

    Chủ Đề