1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P19 pot

5 247 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

  • Contents

  • Introduction

  • Chapter 1: Exploring the PHP Environment

  • Chapter 2: Using Variables and Input

  • Chapter 3: Controlling Your Code with Conditions and Functions

  • Chapter 4: Loops and Arrays

  • Chapter 5: Better Arrays and String Handling

  • Chapter 6: Working with Files

  • Chapter 7: Writing Programs with Objects

  • Chapter 8: XML and Content Management Systems

  • Chapter 9: Using MySQL to Create Databases

  • Chapter 10: Connecting to Databases within PHP

  • Chapter 11: Data Normalization

  • Chapter 12: Building a Three-Tiered Data Application

  • Index

  • Team DDU

Nội dung

68 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r } else if ($roll = = 6){ $binValue = “110”; } else { print “I don’t know that one ”; } // end if print “<br>”; print “<img src = die$roll.jpg>”; print “<br>”; print “In binary, that’s $binValue”; print “<br>”; print “<br>”; print “<br>”; ?> <br> Refresh this page in the browser to roll another die. </body> </html> Using Multiple else if Clauses The Binary Dice program has only one if structure, but that structure has mul- tiple else clauses. The first condition simply checks to see if $roll is equal to 1. If it is, the appropriate code runs, assigning the binary representation of 1 to the $binValue variable. If the first condition is false, the program looks at all the suc- cessive if else clauses until it finds a condition that evaluates to TRUE. If none of the conditions are true, the code in the else clause is executed. You may be surprised that I even put an else clause in this code. Since you know the value of $roll must be between 1 and 6 and you checked each of those val- ues, the program should never need to evaluate the else clause. Things in pro- gramming don’t always work out the way you expect, so it’s a great idea to have some code in an else clause even if you don’t expect to ever need it. It’s much bet- ter to get a message from your program explaining that something unexpected occurred than to have your program blow up inexplicably while your users are using it. The indentation for a multiple-condition if statement is useful so you can tell which parts of the code are part of the if structure, and which parts are meant to be executed if a particular condition turns out to be true. TRICK Using the switch Structure to Simplify Programming The situation in the Binary Dice program happens often enough that another structure is designed for when you are comparing one variable to a number of possible values. The Switch Dice program in Figure 3.9 looks identical to the Binary Dice program as far as the user is concerned, except Switch Dice shows the roll’s Roman numeral representation. While the outward appearance of the last two programs is extremely similar, the underlying code is changed to illustrate a very handy device called the switch structure. This device begins by defining an expression, and then defines a series of branches based on the value of that expression. Building the Switch Dice Program The Switch Dice program code looks different than the Binary Dice code, but the results are the same: <html> <head> <title>Switch Dice</title> </head> <body> 69 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s FIGURE 3.9 This version shows a die roll in Roman numerals. <h1>SwitchDice</h1> <h3>Demonstrates switch structure</h3> <? $roll = rand(1,6); print “You rolled a $roll”; print “<br>”; switch ($roll){ case 1: $romValue = “I”; break; case 2: $romValue = “II”; break; case 3: $romValue = “III”; break; case 4: $romValue = “IV”; break; case 5: $romValue = “V”; break; case 6: $romValue = “VI”; break; default: print “This is an illegal die!”; } // end switch print “<br>”; print “<img src = die$roll.jpg>”; print “<br>”; print “In Roman numerals, that’s $romValue”; print “<br>”; print “<br>”; print “<br>”; 70 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r ?> <br> Refresh this page in the browser to roll another die. </body> </html> Using the switch Structure The switch structure is optimal for when you have one variable to compare against a number of possible values. Use the switch keyword followed, in paren- theses, by the name of the variable you wish to evaluate. A set of braces indicates that the next block of code focuses on evaluating this variable’s possible values. For each possible value, use the case statement, followed by the value, followed by a colon. End each case with a break statement, which indicates the program should stop thinking about this particular case and get ready for the next one. The use of the break statement is probably the trickiest part of using the switch statement—especially if you are familiar with a language such as Visual Basic, which does not require such a construct. It’s important to add the break statement to the end of each case, or the program flow simply “falls through” to the next pos- sible value, even if that value would not otherwise evaluate to true. As a beginner, you should always place the break statement at the end of each case. The last case, which works just like the else clause of the multi-value if state- ment, is called default. It defines code to execute if none of the other cases is active; it’s smart to test for a default case even if you think it is impossible for the computer to get to this default option. Crazy things happen. It’s good to be pre- pared for them. Combining a Form and Its Results Most of your PHP programs up to now have had two distinct files. An HTML file has a form, which calls a PHP program. It can be tedious to keep track of two sep- arate files. Use the if statement to combine both functions into one page. The Hi User program shown in Figures 3.10 and 3.11 looks much like its counter- part in chapter 2, “Using Variables and Input,” but it has an important difference. Rather than being an HTML page and a separate PHP program, the entire program resides in one file on the server. TRAP 71 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s The code for the new version of hiUser shows how to achieve this trick: <html> <head> <title>Hi User</title> </head> 72 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 3.10 The HTML page is actually produced through the “Hi User” PHP code. FIGURE 3.11 The result is produced by exactly the same “Hi User”program. . them. Combining a Form and Its Results Most of your PHP programs up to now have had two distinct files. An HTML file has a form, which calls a PHP program. It can be tedious to keep track of two. Variables and Input,” but it has an important difference. Rather than being an HTML page and a separate PHP program, the entire program resides in one file on the server. TRAP 71 C h a p t e r 3 C o n t r o l l i n g Y o u r C o d e w i t h C o n d i t i o n s a n d F u n c t i o n s The. User</title> </head> 72 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 3.10 The HTML page is actually produced through the “Hi User” PHP code. FIGURE 3.11 The result is produced by exactly the same “Hi User”program.

Ngày đăng: 07/07/2014, 02:20