Học php, mysql và javascript - p 10

Figure 4-1. Program flow is like a single-lane highway There are three types of nonlooping conditionals: the if statement, the switch statement, and the ? operator. By nonlooping, I mean that the actions initiated by the statement take place and program flow then moves on, whereas looping conditionals (which we’ll come to shortly) execute code over and over until a condition has been met. The if Statement One way of thinking about program flow is to imagine it as a single-lane highway that you are driving along. It’s pretty much a straight line, but now and then you encounter various signs telling you. | Figure 4-1. Program flow is like a single-lane highway There are three types of nonlooping conditionals the if statement the switch statement and the operator. By nonlooping I mean that the actions initiated by the statement take place and program flow then moves on whereas looping conditionals which we ll come to shortly execute code over and over until a condition has been met. The if Statement One way of thinking about program flow is to imagine it as a single-lane highway that you are driving along. It s pretty much a straight line but now and then you encounter various signs telling you where to go. In the case of an if statement you could imagine coming across a detour sign that you have to follow if a certain condition is TRUE. If so you drive off and follow the detour until you return to where it started and then continue on your way in your original direction. Or if the condition isn t TRUE you ignore the detour and carry on driving see Figure 4-1 . The contents of the if condition can be any valid PHP expression including equality comparison tests for zero and NULL and even the values returned by functions either built-in functions or ones that you write . The action to take when an if condition is TRUE are generally placed inside curly braces . However you can ignore the braces if you have only a single statement to execute. But if you always use curly braces you ll avoid having to hunt down difficult-to-trace bugs such as when you add an extra line to a condition and it doesn t get evaluated due to lack of braces. Note that for space and clarity many of the examples in this book ignore this suggestion and omit the braces for single statements. In Example 4-19 imagine that it is the end of the month and all your bills have been paid so you are performing some bank account maintenance. Conditionals 71 Example 4-19. An if statement with curly braces php if bank_balance 100 money 1000 bank_balance money In this example you are checking your balance to see .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂ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.