Table of Contents
Acknowledgments xvii
Introduction xix
Part I PHP Basics
Lesson 1 Introduction to PHP 3
A Little History 3
Info for Programmers 4
Why Is PHP so Popular? 5
Downloading and Installing Web Browsers 5
Choosing a Program Editor 6
Installing a PHP Server 7
Summary 9
Self-Test Questions 9
Lesson 2 Incorporating PHP into a Web Page 11
Where to Place the PHP Code 11
In the Document Head 12
In the Document Body 12
Including PHP Files 12
Using Comments 15
Using Semicolons 15
Summary 16
Self-Test Questions 16
Lesson 3 Learning PHP Language Syntax 17
Case Sensitivity 17
Whitespace 13
Variables 18
Variable Naming 19
String Variables 19
Using Quotation Marks in Strings 20
Using Heredoc Strings 21
Escaping Characters 22
Embedding Variables within a String 22
Variable Typing and Casting 24
Summary 24
Self-Test Questions 24
Lesson 4 Using Constants and Superglobals 25
Using Constants 25
Predefined Constants 26
The echo and print Commands 27
Superglobal Variables 27
Superglobals and Security 28
Other PHP Variables 28
Summary 30
Self-Test Questions 30
Lesson 5 Working with Arithmetic Operators 33
Arithmetic Operators 33
Modulus 35
Exponentiation 35
Incrementing and Decrementing Variables 35
Pre-Incrementing 36
Post-Incrementing 36
Pre- and Post-Decrementing 37
Arithmetic Functions 38
Assignment Operators 39
Summary 40
Self-Test Questions 40
Lesson 6 Applying Comparison and Logical Operators 41
Comparison Operators 41
Logical Operators 43
Exclusive Or 45
Boolean Negation 46
The Ternary Operator 46
Bitwise Operators 46
Operator Precedence 46
Operator Associativity 48
Summary 49
Self-Test Questions 49
Lesson 7 Creating Arrays 51
Array Names 52
Creating an Array 52
Assigning Values to an Array Element 52
Using Indexes 53
Retrieving Values 53
Using Array Elements as Indexes 54
Other Ways of Creating Arrays 55
Using Associative Arrays 55
Keys, Values, and Hash Tables 56
Other Ways of Creating an Associative Array 56
Summary 57
Self-Test Questions 58
Lesson 8 Managing Multidimensional Arrays 59
Creating a Two-Dimensional Array 60
Accessing a 'Two-Dimensional Array 62
A More Practical Example 64
Multidimensional Associative Arrays 66
Creating the Multidimensional Array 67
Accessing the Arrays 69
Summary 69
Self-Test Questions 70
Lesson 9 Calling Array Functions 71
Using foreach () 71
Using array-merge () 73
Using implode () 74
The array-walk() Function 75
Using array-push () 76
Using array-pop () 77
Using array-push () and array-pop () Together 78
Using array-reverse () 80
The array-flip () Function 81
Summary 82
Self-Test Questions 82
Lesson 10 Advanced Array Manipulation 83
Using FILO and FIFO Arrays 83
Buffering Using an Array 84
Using array_unshift () and array_shift () 85
Using sort () 85
Reversing a Sort 86
Using array-splice () 87
Removing Elements from an Array 87
Inserting Elements into an Array 88
Advanced Array Splicing 89
Summary 90
Self-Test Questions 91
Lesson 11 Controlling Program Flow 93
The if () Construct 93
Omitting the Braces 94
Positioning of Braces 95
The else Statement 95
The elseif () Construct 97
The switch() Statement 98
Using the break Keyword 100
Using the default Keyword 100
Allowing Fall-Through 101
Summary 102
Self-Test Questions 102
Lesson 12 Looping Sections of Code 105
Using while () Loops 105
The Example in Detail 106
Using do … while () Loops 107
The Example in Detail 108
Using for () Loops 109
The Example in Detail 109
Breaking Out of a Loop 111
Breaking Out of Multiple Loops 112
The continue Statement 112
Continuing within Multiple Loops 113
Summary 114
Self-Test Questions 115
Part II Advanced PHP
Lesson 13 Writing Functions 119
Using Functions 119
The return Statement 120
Passing Arguments 120
Accessing Arguments 121
Handling Unknown Numbers of Arguments 122
Global and Local Variable Scope 124
Using Local Variables 124
The $GLOEALS [] Superglobal Array 127
Global Naming Convention 129
Summary 129
Self-Test Questions 129
Lesson 14 Manipulating Objects 131
OOP Terminology 132
Declaring a Class 132
Creating an Object 133
Accessing Properties and Objects 133
Using a Constructor 134
Destructors 135
Object Cloning 135
Static Methods and Properties 136
Predefined Properties 137
OOP Constants 137
Property and Method Scope 137
Applying Inheritance 139
Using the parent Keyword 140
Writing Subclass Constructors 141
Using the final Keyword 142
Summary 142
Self-Test Questions 143
Lesson 15 Handling Errors and Expressions 145
Error Trapping 145
Regular Expressions 147
Using preg_match () 147
Using preg_match-all () 149
Using preg_replace () 150
Fuzzy Matching 152
Matching Any Character 152
Not Matching a Character 153
Summary 155
Self-Test Questions 155
Lesson 16 Building Web Forms 157
Creating a Form 157
The Difference Between Post and Get Requests 159
Security Issues with Get Requests 159
Accessing Form Data from PHP 160
About register-globals 161
Redisplaying a Form 162
The checkbox and select multiple Input Types 163
Using Hidden Fields 166
Summary 166
Self-Test Questions 166
Lesson 11 Maintaining Security 169
Sanitizing Input 169
Uploading Files to a Server 171
The $-FILES [] Array 172
File Security 173
Other Potential Insecurities 174
Summary 175
Self-Test Questions 175
Lesson 18 Accessing Cookies and Files 177
Using Cookies 177
Setting a Cookie 178
Reading a Cookie 179
Deleting a Cookie 179
Combining These Three Functions 180
Browser Identification 181
The GetBrowser () Function 182
File Handling 183
Testing for a File Existing 184
Creating or Opening a File 185
Writing to a File 185
Closing a File 186
Reading from a File 186
Summary 187
Self-Test Questions 188
Lesson 19 Advanced File Handling 189
Combining File Functions 189
Saving Any Form Data 191
Reading from the Data File 192
Prepopulatnrg the Shopping List 193
The HTML Section 193
Even Simpler File Accessing 194
File Copying 197
File Deleting 197
File Moving 197
Random Access 198
Writing to a Random Access File 199
Managing Directories 199
File Locking 200
A Practical Example 201
Summary 205
Self-Test Questions 205
Lesson 20 Authentication, Sessions, and Ajax 207
Authentication 207
Using Sessions 210
Retrieving Session variables 210
Combining Authentication with Sessions 211
Picking Up Session Details 211
Closing a Session 213
Session Security 213
Using Ajax 214
Creating an Ajax Object 215
The PostAjaxRequest() Function 217
The GetAjaxRequest () Function 218
The callback () Function 219
The ajax.php Program 219
Summary 221
Self-Test Questions 222
Appendix: Answers to the Self-Test Questions 223
Lesson 1 Answers 223
Lesson 2 Answers 224
Lesson 3 Answers 225
Lesson 4 Answers 226
Lesson 5 Answers 226
Lesson 6 Answers 227
Lesson 7 Answers 228
Lesson 8 Answers 229
Lesson 9 Answers 230
Lesson 10 Answers 231
Lesson 11 Answers 231
Lesson 12 Answers 232
Lesson 13 Answers 233
Lesson 14 Answers 234
Lesson 15 Answers 235
Lesson 16 Answers 236
Lesson 17 Answers 237
Lesson 18 Answers 238
Lesson 19 Answers 239
Lesson 20 Answers 240
Index 241