Các loại kiểm soát quyết định có sẵn trong php là gì?

Cấu trúc điều khiển là một khối mã quyết định đường dẫn thực thi của chương trình tùy thuộc vào giá trị của điều kiện đã đặt

Bây giờ chúng ta hãy xem xét một số cấu trúc điều khiển mà PHP hỗ trợ

PHP NẾU Khác

If… then… else là cấu trúc điều khiển đơn giản nhất. Nó đánh giá các điều kiện bằng logic Boolean
Khi nào thì sử dụng if… then… other

  • Bạn có một khối mã chỉ được thực thi nếu một điều kiện nhất định là đúng
  • Bạn có hai lựa chọn, và bạn phải chọn một
  • If… then… other if… được sử dụng khi bạn phải chọn nhiều hơn hai tùy chọn và bạn phải chọn một hoặc nhiều tùy chọn

Cú pháp Cú pháp của if… then… other là;

NƠI ĐÂY,

  • “nếu [điều kiện là đúng]” là cấu trúc điều khiển
  • “khối một” là mã sẽ được thực thi nếu điều kiện là đúng
  • {…else…} là dự phòng nếu điều kiện sai
  • “khối hai” là khối mã được thực thi nếu điều kiện sai

Cách thức hoạt động Biểu đồ dòng dưới đây minh họa cấu trúc điều khiển if then… else hoạt động như thế nào

Hãy xem điều này hoạt động Đoạn mã dưới đây sử dụng “if… then… other” để xác định giá trị lớn hơn giữa hai số

đầu ra

21 is greater than 7

Trường hợp chuyển đổi PHP

Switch…case tương tự như cấu trúc điều khiển if then…else

Nó chỉ thực thi một khối mã duy nhất tùy thuộc vào giá trị của điều kiện

Nếu không có điều kiện nào được đáp ứng thì khối mã mặc định sẽ được thực thi

Nó có cú pháp cơ bản sau

NƠI ĐÂY,

  • “switch[…]{…}” là mã khối cấu trúc điều khiển
  • “giá trị trường hợp. case…” là các khối mã được thực thi tùy thuộc vào giá trị của điều kiện
  • "vỡ nợ. ” là khối mã được thực thi khi không có giá trị nào thỏa mãn điều kiện

Làm thế nào nó hoạt động

Lưu đồ hiển thị bên dưới minh họa cách thức hoạt động của cấu trúc điều khiển công tắc

Ví dụ thực tế

Đoạn mã dưới đây sử dụng cấu trúc điều khiển công tắc để hiển thị thông báo tùy thuộc vào ngày trong tuần

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 những điều kiện này tôi. e. , TRUE hoặc FALSE, 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. Muốn cái gì thì đi đường này, không thì rẽ đường kia. Để sử dụng tính năng này, PHP cung cấp cho chúng ta 4 câu lệnh điều kiện

  • câu lệnh if
  • câu lệnh if…else
  • câu lệnh if…elseif…else
  • tuyên bố chuyển đổi

Bây giờ chúng ta hãy xem xét chi tiết từng vấn đề này

  1. câu lệnh if. Tuyên bố này cho phép chúng tôi thiết lập một điều kiện. Khi là TRUE, khối mã sau được đặt trong mệnh đề if sẽ được thực thi

    cú pháp

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

    Thí dụ



    The number is positive
    
    0

    The number is positive
    
    1
    The number is positive
    
    2

    The number is positive
    
    3

    ________ 54 ________ 55 ________ 51 ________ 57

    The number is positive
    
    8
    The number is positive
    
    9
    50
    51

    52

    53

    đầu ra

    The number is positive
    

    Sơ đồ

  2. câu lệnh if…else. Chúng tôi hiểu rằng nếu một điều kiện sẽ giữ tôi. e. , TRUE, thì khối mã bên trong if sẽ được thực thi. Nhưng nếu điều kiện không ĐÚNG và chúng ta muốn thực hiện một hành động thì sao? . Nếu một điều kiện là TRUE thì nếu khối được thực thi, nếu không thì khối sẽ được thực thi

    cú pháp

    5

    Thí dụ

    The number is positive
    
    0

    The number is positive
    
    1
    56

    The number is positive
    
    3

    ________ 54 ________ 55 ________ 51 ________ 57

    The number is positive
    
    8
    The number is positive
    
    9
    50
    51

    52

    The number is positive
    
    3

    58
    59

    The number is positive
    
    8
    The number is positive
    
    9
    92____151

    52

    53

    đầu ra

    5

    Sơ đồ

  3. if…elseif…else Câu lệnh. Điều này cho phép chúng ta sử dụng nhiều câu lệnh if…else. 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 ĐÚNG.
    Cú pháp.
    9

    Thí dụ

    The number is positive
    
    0

    The number is positive
    
    1
    98
    99
    51

    The number is positive
    
    3

    The number is positive
    
    4
    The number is positive
    
    5
    The number is positive
    
    1
    The number is positive
    
    45 ________ 546
    The number is positive
    
    47

    The number is positive
    
    8
    The number is positive
    
    9
    The number is positive
    
    90
    51

    52

    The number is positive
    
    3

    The number is positive
    
    94
    The number is positive
    
    5
    The number is positive
    
    1
    The number is positive
    
    45
    99
    The number is positive
    
    47

    The number is positive
    
    8
    The number is positive
    
    9
    The number is positive
    
    12____151

    52

    The number is positive
    
    3

    58
    59

    The number is positive
    
    8
    The number is positive
    
    9
    The number is positive
    
    90____151

    52

    53

    đầu ra

    The number is positive
    
    4

    Sơ đồ

  4. tuyên bố chuyển đổi. "Switch" thực hiện trong nhiều trường hợp khác nhau. e. , nó có nhiều trường hợp phù hợp với điều kiện và thực thi một cách thích hợp khối trường hợp cụ thể. Đầu 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 phù hợp thì trường hợp tương tự được thực hiện. Để sử dụng switch chúng ta cần làm quen với 2 từ khóa khác nhau là break và default
    1. 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
    2. 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

    cú pháp



    The number is positive
    
    9

    Thí dụ

    The number is positive
    
    0

    The number is positive
    
    95
    98
    The number is positive
    
    97
    51

    The number is positive
    
    3

    The number is positive
    
    10
    The number is positive
    
    5
    The number is positive
    
    95
    The number is positive
    
    47

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    46
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    00____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    97
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    11____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    18
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    22____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    29____917

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    33____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    40
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    44
    51

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    51
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    55
    51

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    62______917

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    66____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    99
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    77____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    84
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    88
    51

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    The number is positive
    
    95
    The number is positive
    
    17

    The number is positive
    
    18
    The number is positive
    
    9
    The number is positive
    
    99____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    506____917

    The number is positive
    
    18
    The number is positive
    
    9
    510____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    The number is positive
    
    8
    The number is positive
    
    15
    517____917

    The number is positive
    
    18
    The number is positive
    
    9
    521____151

    The number is positive
    
    18
    The number is positive
    
    03
    51

    _______58____1527____917

    The number is positive
    
    18
    The number is positive
    
    9
    531____151

    52

    53

    đầu ra

    The number is positive
    
    1

    Sơ đồ

Toán tử bậc ba

Ngoài tất cả các câu lệnh điều kiện này, PHP còn cung cấp một cách viết tắt if…else, được gọi là Toán tử bậc ba. Câu lệnh sử dụng dấu chấm hỏi [?] và dấu hai chấm [. ] và nhận ba toán hạng. một điều kiện để kiểm tra, một kết quả cho TRUE và một kết quả cho FALSE.
Cú pháp.

The number is positive
9

Thí dụ

The number is positive
0

The number is positive
1
56

The number is positive
3

________ 54 ________ 55 ________ 51 ________ 57

The number is positive
8
The number is positive
9
545____151

52

58 
59

The number is positive
8
The number is positive
9
552____151

52

The number is positive
3

556

557

The number is positive
9
The number is positive
5
The number is positive
1
561
562 
563

564
565
51

53

đầu ra

The number is positive
1

Vui lòng viết bình luận nếu bạn 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ủ đề thảo luận ở trên

Câu lệnh kiểm soát quyết định trong PHP là gì?

Câu lệnh điều kiện PHP cho phép bạn đưa ra quyết định, dựa trên kết quả của một điều kiện . Những tuyên bố này được gọi là Tuyên bố ra quyết định hoặc Tuyên bố có điều kiện.

Có bao nhiêu loại câu lệnh điều khiển trong PHP?

Có hai loại cơ bản của loại Câu lệnh điều khiển đầu tiên trong PHP [câu lệnh điều kiện] trong bất kỳ ngôn ngữ lập trình nào, Câu lệnh IF, ELSE và ELSEIF. Tuyên bố CHUYỂN ĐỔI

Có bao nhiêu quyết định của câu lệnh Control?

Chúng tôi có ba loại chính câu lệnh kiểm soát quyết định mà chúng tôi sử dụng trong ngôn ngữ lập trình C.

Câu lệnh rẽ nhánh trong PHP là gì?

Các câu lệnh rẽ nhánh cho phép chúng tôi kiểm soát luồng chương trình . Ví dụ: bạn chỉ muốn chạy một đoạn mã nếu một điều kiện nhất định đúng trong tình huống này, các câu lệnh phân nhánh có thể giúp ích cho bạn rất nhiều. Dưới đây là danh sách các câu lệnh phân nhánh có sẵn trong PHP.

Chủ Đề