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:
- 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
0The number is positive
1The number is positive
2The number is positive
3The number is positive
4The number is positive
1The number is positive
6The number is positive
7The number is positive
8The number is positive
9if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
1if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
2Output:
The number is positive
Flowchart::
- 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
0The number is positive
1if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
5The number is positive
3The number is positive
4The number is positive
1The number is positive
6The number is positive
7The number is positive
8The number is positive
9if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
1The number is negative
5The number is negative
6Nế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 }
1if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
2Output:
The number is positive
1if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
5Flowchart::
The number is positive
7The number is positive
8The number is negative
9if [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
0The 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
1if [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 }
5if [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 }
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
1The number is positive
3The number is positive
4The number is positive
1Happy Independence Day!!!
1Happy Independence Day!!!
22The number is positive
7The number is positive
8Happy Independence Day!!!
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
1The number is negative
5The number is negative
6Happy Independence Day!!!
9The number is positive
4The number is positive
1Happy Independence Day!!!
1if [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 }
6Happy Independence Day!!!
3if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
1if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
2Output:
Happy Independence Day!!!
Flowchart::
The number is positive
7The number is positive
8switch[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;
7if [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.The number is positive
7The number is positive
8Its February
4if [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.- 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
0Câ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;
3The number is positive
4Its February
9Happy Independence Day!!!
3Câ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
9if [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;
1if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8Happy Independence Day!!!
22The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is negative The number is negative
3if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8[condition] ? if TRUE execute this : otherwise execute this;
1The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
04if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
11The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
15if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
222The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
26if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
33The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
37if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
44The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
48if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
55The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
59if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8if [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 }
6The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
70if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
777____90The number is negative The number is negative
1The number is positive
8The number is positive
81if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
88The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8The number is positive
92if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8The number is positive
99The number is negative The number is negative
0The number is negative The number is negative
1The number is positive
8if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
03if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is negative The number is negative
1The number is negative The number is negative
6if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
0The number is positive
7if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
20The number is negative The number is negative
0The number is positive
7[condition] ? if TRUE execute this : otherwise execute this;
8if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
10The number is negative The number is negative
0if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
1if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }
2Output:
Its February
Flowchart::
The number is negative
The number is negative
1The number is positive
8 if [condition] {
// if TRUE then execute this code
}
else{
// if FALSE then execute this code
}
14if [condition] {
// if TRUE then execute this code
}
else{
// if FALSE then execute this code
}
0
The number is negative The number is negative1
The number is positive8
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 positive0
The number is positive1
if [condition] { // if TRUE then execute this code } else{ // if FALSE then execute this code }5
The number is positive3
The number is positive4
The number is positive1
The number is positive6
The number is positive7
The number is positive8
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 negative5
The number is negative6
The number is positive7
The number is positive8
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 positive8
The number is positive4
The number is positive1
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.