How to do everything with PHP (phần 3)

Tham khảo tài liệu 'how to do everything with php (phần 3)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 84 How to Do Everything with PHP MySQL Here the if-elseif-else control structure assigns a different value to the capital variable depending on the country code. As soon as one of the if branches within the block is found to be true PHP will execute the corresponding code skip the remaining if statements in the block and jump immediately to the lines following the entire if-elseif-else block. Using the switch Statement An alternative to the if-else family of control structures is PHP s switchcase statement which does almost the same thing. Here a switch statement evaluates a conditional expression or decision variable depending on the result of the evaluation an appropriate case block is executed. If no matches can be found a default block is executed instead. Here is what the syntax of this construct looks like php switch condition variable case possible result 1 do this case possible result 2 do this . case possible result n do this case default do this Here s a rewrite of the last example using switch-case php switch country case UK capital London break case US capital Washington break TEAM Line CHAPTER 4 Using Conditional Statements and Loops 85 case FR capital break default capital break Paris Unknown 4 A couple of important keywords are here the break keyword is used to break out of the switch statement block and move immediately to the lines following it while the default keyword is used to execute a default set of statements when the variable passed to switch does not satisfy any of the conditions listed within the block. Read more about the break keyword in the section entitled Controlling Loop Iteration with break and continue later in this chapter. NOTE If you forget to break out of a case block PHP will continue executing the code in each subsequent case block until it reaches the end of the switch block. The Ternary Operator PHP s ternary operator represented by a question mark is aptly named the first time you see it you re sure to wonder what exactly

Không thể tạo bản xem trước, hãy bấm tải xuống
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.