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
- 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
0The number is positive
1The number is positive
2The number is positive
3________ 54 ________ 55 ________ 51 ________ 57
The number is positive
8The number is positive
9 50515253đầu ra
The number is positive
Sơ đồ
- 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
5Thí dụ
The number is positive
0The number is positive
1 56The number is positive
3________ 54 ________ 55 ________ 51 ________ 57
The number is positive
8The number is positive
9 505152The number is positive
35859The number is positive
8The number is positive
9 92____1515253đầu ra
5Sơ đồ
- 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. 9Thí dụ
The number is positive
0The number is positive
1 989951The number is positive
3The number is positive
4The number is positive
5The number is positive
1The number is positive
45 ________ 546The number is positive
47The number is positive
8The number is positive
9The number is positive
905152The number is positive
3The number is positive
94The number is positive
5The number is positive
1The number is positive
4599The number is positive
47The number is positive
8The number is positive
9The number is positive
12____15152The number is positive
35859The number is positive
8The number is positive
9The number is positive
90____1515253đầu ra
The number is positive
4Sơ đồ
- 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
- 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
- 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
9Thí dụ
The number is positive
0The number is positive
95 98The number is positive
9751The number is positive
3The number is positive
10The number is positive
5The number is positive
95The number is positive
47The number is positive
8The number is positive
15The number is positive
46The number is positive
17The number is positive
18The number is positive
9The number is positive
00____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
97The number is positive
17The number is positive
18The number is positive
9The number is positive
11____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
18The number is positive
17The number is positive
18The number is positive
9The number is positive
22____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
29____917The number is positive
18The number is positive
9The number is positive
33____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
40The number is positive
17The number is positive
18The number is positive
9The number is positive
4451The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
51The number is positive
17The number is positive
18The number is positive
9The number is positive
5551The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
62______917The number is positive
18The number is positive
9The number is positive
66____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15 99The number is positive
17The number is positive
18The number is positive
9The number is positive
77____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
84The number is positive
17The number is positive
18The number is positive
9The number is positive
8851The number is positive
18The number is positive
0351The number is positive
8The number is positive
15The number is positive
95The number is positive
17The number is positive
18The number is positive
9The number is positive
99____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15 506____917The number is positive
18The number is positive
9 510____151The number is positive
18The number is positive
0351The number is positive
8The number is positive
15 517____917The number is positive
18The number is positive
9 521____151The number is positive
18The number is positive
0351_______58____1527____917
The number is positive
18The number is positive
9 531____1515253đầu ra
The number is positive
1Sơ đồ
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 positive9
Thí dụ
The number is positive0
The number is positive1 56
The number is positive3
________ 54 ________ 55 ________ 51 ________ 57
The number is positive8
The number is positive9 545____1515258 59
The number is positive8
The number is positive9 552____15152
The number is positive3556557
The number is positive9
The number is positive5
The number is positive1 561562 5635645655153
đầu ra
The number is positive1
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