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.
Bạn có thể sử dụng các câu lệnh có điều kiện trong mã của bạn để đưa ra quyết định của bạn. PHP hỗ trợ sau ba tuyên bố ra quyết định -
Nếu ... câu lệnh khác - Sử dụng câu lệnh này nếu bạn muốn thực thi một tập hợp mã khi một điều kiện là đúng và một điều khác nếu điều kiện không đúng − use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
câu lệnh otherif - được sử dụng với câu lệnh if ... khác để thực thi một tập hợp mã nếu một trong một số điều kiện là đúng − is used with the if...else statement to execute a set of code if one of the several condition is true
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 của IF..elseif..else mã. − 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.
Tuyên bố nếu ... khác
Nếu bạn muốn thực thi một số mã nếu một điều kiện là đúng và một mã khác nếu một điều kiện là sai, hãy sử dụng câu lệnh if .... other khác.
Cú pháp
if [condition] code to be executed if condition is true; else code to be executed if condition is false;
Thí dụ
Ví dụ sau đây sẽ xuất hiện "Chúc một ngày cuối tuần vui vẻ!" Nếu ngày hiện tại là thứ Sáu, nếu không, nó sẽ xuất hiện "Chúc một ngày tốt lành!":
Nó sẽ tạo ra kết quả sau -
Have a nice weekend!
Tuyên bố khác
Nếu bạn muốn thực thi một số mã nếu một trong một số điều kiện là đúng, hãy sử dụng câu lệnh otherif
Cú pháp
if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
Thí dụ
Ví dụ sau đây sẽ xuất hiện "Chúc một ngày cuối tuần vui vẻ!" Nếu ngày hiện tại là thứ Sáu, nếu không, nó sẽ xuất hiện "Chúc một ngày tốt lành!":
Nó sẽ tạo ra kết quả sau -
Have a nice Weekend!
Tuyên bố khác
Nếu bạn muốn thực thi một số mã nếu một trong một số điều kiện là đúng, hãy sử dụng câu lệnh otherif
Thí dụ
Cú pháp
switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
Thí dụ
Ví dụ sau đây sẽ xuất hiện "Chúc một ngày cuối tuần vui vẻ!" Nếu ngày hiện tại là thứ Sáu, nếu không, nó sẽ xuất hiện "Chúc một ngày tốt lành!":
Nó sẽ tạo ra kết quả sau -
Today is Monday
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
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
- 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:
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.
Have a nice weekend!
2Have a nice weekend!
3Have a nice weekend!
0Have a nice weekend!
5Have a nice weekend!
6Have a nice weekend!
7Have a nice weekend!
46Have a nice weekend!
9if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
0if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
1Output:
Flowchart::
Syntax::
Have a nice weekend!
6Have a nice weekend!
7Have a nice weekend!
53Have a nice weekend!
9Have a nice weekend!
0if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
4Have a nice weekend!
2Have a nice weekend!
3Have a nice weekend!
0Have a nice weekend!
5Have a nice weekend!
6Have a nice weekend!
7Have a nice weekend!
46Have a nice weekend!
9if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
0if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
0if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
1Output:
Flowchart::
- 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.: This allows us
to use multiple if…else statements. We use this when there are multiple conditions of TRUE cases.
Syntax:Example:
Have a nice weekend!
0Have a nice Weekend!
4Have a nice Weekend!
55Have a nice weekend!
9Have a nice weekend!
2Have a nice weekend!
3Have a nice weekend!
0switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
0switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
1switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
2Have a nice weekend!
6Have a nice weekend!
7switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
5Have a nice weekend!
9if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
0switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
8Have a nice weekend!
3Have a nice weekend!
0switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
0Have a nice Weekend!
5switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
2Have a nice weekend!
6Have a nice weekend!
7Have a nice weekend!
9if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
0Have a nice weekend!
6Have a nice weekend!
7Today is Monday
3Have a nice weekend!
9if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
0if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
1Output:
Flowchart::
- 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.: 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.
- 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.break statement is used to stop the automatic control flow into the next cases and exit from the switch case.
- 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.default statement contains the code that would execute if none of the cases match.
Syntax::
Example:
Today is Monday
8Have a nice Weekend!
4if [condition]{ // if TRUE then execute this code }
0Have a nice weekend!
9if [condition]{ // if TRUE then execute this code }
2Have a nice weekend!
3Today is Monday
8switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
2Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7switch [expression]{ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
1if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
0if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7Have a nice Weekend!
55____99Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
01Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7Have a nice weekend!
08if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
1212Have a nice weekend!
9Have a nice weekend!
6if [condition]{ // if TRUE then execute this code }
7Have a nice weekend!
19if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
23Have a nice weekend!
9Have a nice weekend!
9Have a nice weekend!
6Have a nice weekend!
29if [condition]{ // if TRUE then execute this code }
9Have a nice weekend!
7Have a nice weekend!
33Have a nice weekend!
9if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
0if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;
1Output:
Flowchart::
Các nhà khai thác ternary
Ngoài tất cả các tuyên bố có điều kiện này, PHP còn cung cấp một cách viết tốc ký nếu khác, được gọi là các toán tử ternary. Tuyên bố sử dụng dấu hỏi [?] Và một dấu hai chấm [:] và mất ba toán hạng: một điều kiện để kiểm tra, kết quả cho sự thật và kết quả cho false.syntax:
Syntax:
7
Example:
9
Have a nice weekend!0
if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;4
Have a nice weekend!2
Have a nice weekend!3
Have a nice weekend!0
Have a nice weekend!5
Have a nice weekend!6
Have a nice weekend!7
Have a nice weekend!46
Have a nice weekend!9
if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;0
4
5
Have a nice weekend!6
Have a nice weekend!7
Have a nice weekend!53
Have a nice weekend!9
if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;0
Have a nice weekend!7
Have a nice weekend!3
Have a nice weekend!0
Have a nice weekend!59
Have a nice weekend!60
Have a nice weekend!61
Have a nice weekend!62
Have a nice weekend!63
Have a nice weekend!9
if [condition] code to be executed if condition is true; elseif [condition] code to be executed if condition is true; else code to be executed if condition is false;1
Output:
8
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.