Danh mục

Giải pháp thiết kế web động với PHP - p 7

Số trang: 10      Loại file: pdf      Dung lượng: 526.28 KB      Lượt xem: 10      Lượt tải: 0    
Thư Viện Số

Hỗ trợ phí lưu trữ khi tải xuống: 3,000 VND Tải xuống file đầy đủ (10 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

HOW TO WRITE PHP SCRIPTS} elseif ( second condition is true ) { // code to be executed if first condition fails // but second condition is true } else { // default code if both conditions are false } You can use as many elseif clauses in a conditional statement as you like. It s important to note that only the first one that equates to true will be executed; all others will be ignored, even if they re also true. This means you need to build conditional statements in the order of priority that you want them to...
Nội dung trích xuất từ tài liệu:
Giải pháp thiết kế web động với PHP - p 7 HOW TO WRITE PHP SCRIPTS } elseif ( second condition is true ) { // code to be executed if first condition fails // but second condition is true } else { // default code if both conditions are false } You can use as many elseif clauses in a conditional statement as you like. It s important to note that only the first one that equates to true will be executed; all others will be ignored, even if they re also true. This means you need to build conditional statements in the order of priority that you want them to be evaluated. It s strictly a first-come, first-served hierarchy. Although elseif is normally written as one word, you can use else if as separate words. An alternative decision-making structure, the switch statement, is described in the second half of this chapter.Making comparisons Conditional statements are interested in only one thing: whether the condition being tested equates to true. If it s not true, it must be false. There s no room for half-measures or maybes. Conditions often depend on the comparison of two values. Is this bigger than that? Are they both the same? And so on. To test for equality, PHP uses two equal signs (==) like this: if ($status == administrator) { // send to admin page } else { // refuse entry to admin area } Don t use a single equal sign in the first line ( $status = administrator ). Doing so will open the admin area of your website to everyone. Why? Because this automatically sets the value of $status to administrator ; it doesn t compare the two values. To compare values, you must use two equal signs. It s an easy mistake to make, but one with potentially disastrous consequences. Size comparisons are performed using the mathematical symbols for less than (). Let s say you re checking the size of a file before allowing it to be uploaded to your server. You could set a maximum size of 50kB like this (1 kilobyte = 1024 bytes): if ($bytes > 51200) { // display error message and abandon upload } else { // continue upload } You can test for multiple conditions simultaneously. Details are in the second half of this chapter. 41 CHAPTER 3 Using indenting and whitespace for clarity Indenting code helps to keep statements in logical groups, making it easier to understand the flow of the script. There are no fixed rules; PHP ignores any whitespace inside code, so you can adopt any style you like. The important thing is to be consistent so that you can spot anything that looks out of place. The limited width of the printed page means that I normally use just two spaces to indent code in this book, but most people find that tabbing four or five spaces makes for the most readable code. Perhaps the biggest difference in styles lies in the way individual developers arrange curly braces. I put the opening curly brace of a code block on the same line as the preceding code, and put the closing brace on a new line after the code block, like this: if ($bytes > 51200) { // display error message and abandon upload } else { // continue upload } However, others prefer this style: if ($bytes > 51200)Download from Wow! eBook { // display error message and abandon upload } else { // continue upload } The style isn t important. What matters is that your code is consistent and easy to read. Using loops for repetitive tasks Loops are hug ...

Tài liệu được xem nhiều: