TAILIEUCHUNG - Phát triển web với PHP và MySQL - p 8

PHP Crash Course CHAPTER 1 echo $num.””; $num++; } 45 1 PHP CRASH COURSE At the beginning of each iteration, the condition is tested. If the condition is false, the block will not be executed and the loop will end. The next statement after the loop will then be executed. We can use a while loop to do something more useful, such as display the repetitive freight table in Figure . Listing uses a while loop to generate the freight table. LISTING —Generating Bob’s Freight Table with PHP Distance Cost \n $distance\n”; echo “ ”. $distance / 10 .”\n\n”; $distance +=. | PHP Crash Course Chapter 1 45 echo num. BR num At the beginning of each iteration the condition is tested. If the condition is false the block will not be executed and the loop will end. The next statement after the loop will then be executed. We can use a while loop to do something more useful such as display the repetitive freight table in Figure . Listing uses a while loop to generate the freight table. 1 PHP Crash Course Listing Generating Bob s Freight Table with PHP body table border 0 cellpadding 3 tr td bgcolor CCCCCC align center Distance td td bgcolor CCCCCC align center Cost td tr distance 50 while distance 250 echo tr n td align right distance td n echo td align right . distance 10 . td n tr n distance 50 table body html for Loops The way that we used the while loops previously is very common. We set a counter to begin with. Before each iteration we tested the counter in a condition. At the end of each iteration we modified the counter. We can write this style of loop in a more compact form using a for loop. 46 Using PHP Part I The basic structure of a for loop is for expressionl condition expression expression expressionl is executed once at the start. Here you will usually set the initial value of a counter. The condition expression is tested before each iteration. If the expression returns false iteration stops. Here you will usually test the counter against a limit. expression2 is executed at the end of each iteration. Here you will usually adjust the value of the counter. expressions is executed once per iteration. This expression is usually a block of code and will contain the bulk of the loop code. We can rewrite the while loop example in Listing as a for loop. The PHP code will become for distance 50 distance 250 distance 50 echo tr n td align right distance td n echo td align right . distance 10 . td n tr n Both the while version and the for version are functionally identical. The for loop is somewhat more compact .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.