Tải bản đầy đủ (.pdf) (5 trang)

PHP 5/MySQL Programming- P3 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (58.82 KB, 5 trang )

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Chapter 1: Exploring the PHP Environment . . . . . . .1
Introducing the Tip of the Day Program. . . . . . . . . . . . . . . . . . 2
Programming on the Web Server . . . . . . . . . . . . . . . . . . . . . . . 3
Installing PHP and Apache. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Using an Existing Server . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Installing Your Own Development Environment . . . . . . . . 5
Installing Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Installing Apache Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Testing Your Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Starting Apache as a Service . . . . . . . . . . . . . . . . . . . . . . . . 8
Configuring Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Running Your Local Server . . . . . . . . . . . . . . . . . . . . . . . . . 9
Installing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Downloading the PHP Program . . . . . . . . . . . . . . . . . . . . 10
Telling Apache about PHP . . . . . . . . . . . . . . . . . . . . . . . . . 11
Adding PHP to Your Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Adding PHP Commands to an HTML Page . . . . . . . . . . . 12
Examining the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Configuring Your Version of PHP . . . . . . . . . . . . . . . . . . . . . . 15
Safe Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Register Globals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Windows Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Creating the Tip of the Day Program . . . . . . . . . . . . . . . . . . . 18
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter 2: Using Variables and Input . . . . . . . . . .21
Introducing the Story Program . . . . . . . . . . . . . . . . . . . . . . . . 22
Using Variables in Your Scripts. . . . . . . . . . . . . . . . . . . . . . . . 23
Introducing the Hi Jacob Program . . . . . . . . . . . . . . . . . . 23
Creating a String Variable . . . . . . . . . . . . . . . . . . . . . . . . . 25
Printing a Variable’s Value . . . . . . . . . . . . . . . . . . . . . . . . . 26


Using the Semicolon to End a Line. . . . . . . . . . . . . . . . . . 27
C
on
t
e
n
t
s
Using Variables for More-Complex Pages . . . . . . . . . . . . . . . 28
Building the Row Your Boat Page . . . . . . . . . . . . . . . . . . . 28
Creating Multi-Line Strings . . . . . . . . . . . . . . . . . . . . . . . . 29
Working with Numeric Variables . . . . . . . . . . . . . . . . . . . . . . 30
Making the ThreePlusFive Program . . . . . . . . . . . . . . . . . 30
Assigning Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . 32
Using Mathematical Operators . . . . . . . . . . . . . . . . . . . . . 32
Creating a Form to Ask a Question. . . . . . . . . . . . . . . . . . . . . 33
Building an HTML Page with a Form . . . . . . . . . . . . . . . . 34
Setting the Action Attribute to a Script File . . . . . . . . . . . 35
Writing a Script to Retrieve the Data. . . . . . . . . . . . . . . . . 35
Sending Data without a Form . . . . . . . . . . . . . . . . . . . . . . . . . 36
Understanding the get Method . . . . . . . . . . . . . . . . . . . . . 36
Using a URL to Embed Form Data . . . . . . . . . . . . . . . . . . 38
Working with Multiple Field Queries. . . . . . . . . . . . . . . . . 40
Reading Input from Other Form Elements. . . . . . . . . . . . . . . 40
Introducing the borderMaker Program . . . . . . . . . . . . . . . 40
Building the borderMaker.html Page . . . . . . . . . . . . . . . . 41
Reading the Form Elements. . . . . . . . . . . . . . . . . . . . . . . . 43
Returning to the Story Program . . . . . . . . . . . . . . . . . . . . . . . 46
Designing the Story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Building the HTML Page. . . . . . . . . . . . . . . . . . . . . . . . . . . 48

Checking the Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Building the Final Story . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Chapter 3: Controlling Your Code with
Conditions and Functions . . . . . . . . . . .55
Examining the Petals Around the Rose Game . . . . . . . . . . . 56
Creating a Random Number . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Viewing the Roll Em Program . . . . . . . . . . . . . . . . . . . . . . 57
Printing a Corresponding Image . . . . . . . . . . . . . . . . . . . . 58
Using the if Statement to Control Program Flow . . . . . . . . . 58
Introducing the Ace Program. . . . . . . . . . . . . . . . . . . . . . . 59
Creating a Condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Exploring Comparison Operators . . . . . . . . . . . . . . . . . . . 62
Creating an if Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Working with Negative Results . . . . . . . . . . . . . . . . . . . . . . . . 63
Demonstrating the Ace or Not Program . . . . . . . . . . . . . . 64
Using the else Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
xi
C
o
n
ten
t
s
Working with Multiple Values . . . . . . . . . . . . . . . . . . . . . . . . . 66
Writing the Binary Dice Program. . . . . . . . . . . . . . . . . . . . 66
Using Multiple else if Clauses . . . . . . . . . . . . . . . . . . . . . . 68
Using the switch Structure to Simplify Programming . . . . . 69
Building the Switch Dice Program . . . . . . . . . . . . . . . . . . 69
Using the switch Structure. . . . . . . . . . . . . . . . . . . . . . . . . 71

Combining a Form and Its Results . . . . . . . . . . . . . . . . . . . . . 71
Responding to Checkboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Using Functions to Encapsulate Parts of the Program . . . . . 77
Examining the This Old Man Program . . . . . . . . . . . . . . . 77
Creating New Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Using Parameters and Function Values . . . . . . . . . . . . . . . . . 80
Examining the Param.php Program . . . . . . . . . . . . . . . . . 80
Looking at Encapsulation in the Main Code Body. . . . . . 82
Returning a Value: The chorus() Function . . . . . . . . . . . . 83
Accepting a Parameter in the verse() Function . . . . . . . . 84
Managing Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Looking at the Scope Demo. . . . . . . . . . . . . . . . . . . . . . . . 86
Returning to the Petals Game . . . . . . . . . . . . . . . . . . . . . . . . . 88
Starting HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Main Body Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
The printGreeting() Function . . . . . . . . . . . . . . . . . . . . . . . 89
The printDice() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The showDie() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
The calcNumPetals Function . . . . . . . . . . . . . . . . . . . . . . . 92
The printForm() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The Ending HTML Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Chapter 4: Loops and Arrays . . . . . . . . . . . . . . . . . .95
Introducing the Poker Dice Program . . . . . . . . . . . . . . . . . . . 96
Counting with the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Initializing a Sentry Variable. . . . . . . . . . . . . . . . . . . . . . . . 98
Setting a Condition to Finish the Loop . . . . . . . . . . . . . . . 99
Changing the Sentry Variable . . . . . . . . . . . . . . . . . . . . . . 99
Building the Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Modifying the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Counting by Fives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Counting Backwards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Using a while Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
xii
C
o
n
ten
t
s
Repeating Code with a while Loop . . . . . . . . . . . . . . . . . 103
Recognizing Endless Loops . . . . . . . . . . . . . . . . . . . . . . . 105
Building a Well-Behaved Loop. . . . . . . . . . . . . . . . . . . . . 106
Working with Basic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Generating a Basic Array . . . . . . . . . . . . . . . . . . . . . . . . . 109
Using a Loop to Examine an Array’s Contents . . . . . . . 109
Using the array() Function to Preload an Array . . . . . . . 110
Detecting the Size of an Array . . . . . . . . . . . . . . . . . . . . . 110
Improving This Old Man with Arrays and Loops . . . . . . . . 111
Building the Place Array. . . . . . . . . . . . . . . . . . . . . . . . . . 113
Writing Out the Lyrics. . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Keeping Persistent Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Counting with Form Fields. . . . . . . . . . . . . . . . . . . . . . . . 114
Storing Data in the Text Box . . . . . . . . . . . . . . . . . . . . . . 116
Using a Hidden Field for Persistence . . . . . . . . . . . . . . . 117
Writing the Poker Dice Program . . . . . . . . . . . . . . . . . . . . . . 117
Setting Up the HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Building the Main Code Body . . . . . . . . . . . . . . . . . . . . . 118
Making the rollDice() Function. . . . . . . . . . . . . . . . . . . . . 119
Creating the evaluate() Function . . . . . . . . . . . . . . . . . . . 123

Printing the Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Chapter 5: Better Arrays and String Handling . .133
Introducing the Word Search Program Creator. . . . . . . . . . 134
Using the foreach Loop to Work with an Array. . . . . . . . . . 135
Introducing the foreach.php Program. . . . . . . . . . . . . . . 136
Creating an Associative Array. . . . . . . . . . . . . . . . . . . . . . . . 137
Examining the assoc.php Program . . . . . . . . . . . . . . . . . 138
Building an Associative Array . . . . . . . . . . . . . . . . . . . . . 138
Building an Associative Array
with the array() Function . . . . . . . . . . . . . . . . . . . . . . . 139
Using foreach with Associative Arrays . . . . . . . . . . . . . . 140
Using Built-In Associative Arrays . . . . . . . . . . . . . . . . . . . . . 141
Introducing the formReader.php Program . . . . . . . . . . . 141
Reading the $_REQUEST Array . . . . . . . . . . . . . . . . . . . . 141
Creating a Multidimensional Array. . . . . . . . . . . . . . . . . . . . 144
Building the HTML for the Basic
Multidimensional Array . . . . . . . . . . . . . . . . . . . . . . . . 146
Responding to the Distance Query . . . . . . . . . . . . . . . . . 147
xiii
C
o
n
ten
t
s
Making a Two-Dimensional Associative Array . . . . . . . . . . 150
Building the HTML for the Associative Array. . . . . . . . . 150
Responding to the Query . . . . . . . . . . . . . . . . . . . . . . . . . 151
Building the Two-Dimensional Associative Array . . . . . 153

Getting Data from the Two-Dimensional
Associative Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Manipulating String Values . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Demonstrating String Manipulation with
the Pig Latin Translator . . . . . . . . . . . . . . . . . . . . . . . . 154
Building the Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Using the split() Function to Break a
String into an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Trimming a String with rtrim(). . . . . . . . . . . . . . . . . . . . . 157
Finding a Substring with substr() . . . . . . . . . . . . . . . . . . 158
Using strstr() to Search for One
String Inside Another . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Using the Concatenation Operator . . . . . . . . . . . . . . . . . 159
Finishing the Pig Latin Program . . . . . . . . . . . . . . . . . . . 159
Translating Between Characters and ASCII Values . . . . 159
Returning to the Word Search Creator. . . . . . . . . . . . . . . . . 160
Getting the Puzzle Data from the User . . . . . . . . . . . . . . 160
Setting Up the Response Page . . . . . . . . . . . . . . . . . . . . 161
Working with the Empty Data Set . . . . . . . . . . . . . . . . . . 162
Building the Program’s Main Logic . . . . . . . . . . . . . . . . 163
Parsing the Word List . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Clearing the Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Filling the Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Adding a Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Making a Puzzle Board . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Adding the Foil Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Printing the Puzzle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Printing the Answer Key. . . . . . . . . . . . . . . . . . . . . . . . . . 178
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Chapter 6: Working with Files . . . . . . . . . . . . . . . .181

Previewing the Quiz Machine . . . . . . . . . . . . . . . . . . . . . . . . 182
Entering the Quiz Machine System . . . . . . . . . . . . . . . . . 182
Editing a Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Taking a Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Seeing the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
xiv
C
o
n
ten
t
s

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×