Saturday, 4 July 2015

Teach Yourself PHP4

SAMS Teach Yourself PHP4 in 24 Hours 2
Acknowledgments 4
Tell Us What You Think! 4
Introduction 6
Who Should Read This Book? 6
How This Book Is Organized 6
Hour 1: PHP: From Home Page to Portal 22
Overview 22
What Is PHP? 22
How Did PHP Evolve? 23
What's New in PHP4 24
The Zend Engine 24
Why Choose PHP? 25
Speed of Development 25
PHP Is Open Source 26
Performance 26
Portability 26
Summary 27
Q&A 27
Workshop 27
Quiz 27
Activity 28
Hour 2: Installing PHP 29
Overview 29
Platforms, Servers, Databases, and PHP 29
Where to Find PHP and More 30
Installing PHP4 for Linux and Apache 30
Some configure Options 32
--enable-track-vars 32
--with-gd 32
--with-mysql 33
Configuring Apache 33
php.ini 34
short_open_tag 35
Error Reporting Directives 35
Variable Directives 36
Help! 36
Summary 38
Q&A 38
Workshop 38
Quiz 38
Activity 39
Hour 3: A First Script 40
Overview 40
Our First Script 40
Beginning and Ending a Block of PHP Statements 42
The print() Function 44
Combining HTML and PHP 44
Adding Comments to PHP Code 46
Summary 47
Q&A 47
Workshop 47
Quiz 48
Activity 48
Hour 4: The Building Blocks 49
Overview 49
Variables 49
Dynamic Variables 50
References to Variables 52
Data Types 53
Changing Type with settype() 55
Changing Type by Casting 57
Operators and Expressions 58
The Assignment Operator 59
Arithmetic Operators 59
The Concatenation Operator 60
More Assignment Operators 60
Comparison Operators 61
Creating More Complex Test Expressions with the Logical
Operators 62
Automatically Incrementing and Decrementing an Integer
Variable 63
Operator Precedence 65
Constants 66
Predefined Constants 67
Summary 67
Q&A 67
Workshop 68
Quiz 68
Activities 69
Hour 5: Going with the Flow 70
Overview 70
Switching Flow 70
The if Statement 70
Using the else Clause with the if Statement 72
Using the elseif Clause with the if Statement 73
The switch Statement 75
Using the ? Operator 77
Loops 78
The while Statement 78
The do..while Statement 79
The for Statement 80
Breaking Out of Loops with the break Statement 82
Skipping an Iteration with the continue Statement 84
Nesting Loops 85
Summary 86
Q&A 86
Hour 6: Functions 88
Overview 88
What Is a Function? 88
Calling Functions 88
Defining a Function 90
Returning Values from User-Defined Functions 92
Dynamic Function Calls 93
Variable Scope 94
Accessing Variables with the global Statement 95
Saving State Between Function Calls with the static Statement
98
More About Arguments 100
Setting Default Values for Arguments 100
Passing References to Variables to Functions 102
Summary 105
Q&A 105
Workshop 105
Quiz 106
Activity 107
Hour 7: Arrays 108
Overview 108
What Is an Array? 108
Creating Arrays 109
Defining Arrays with the array() Function 109
Defining or Adding to Arrays with the Array Identifier 110
Associative Arrays 110
Defining Associative Arrays with the array() Function 111
Directly Defining or Adding to an Associative Array 111
Multidimensional Arrays 112
Accessing Arrays 113
Getting the Size of an Array 113
Looping Through an Array 114
Looping Through an Associative Array 115
Outputting a Multidimensional Array 116
Manipulating Arrays 118
Joining Two Arrays with array_merge() 119
Adding Multiple Variables to an Array with array_push()
119
Removing the First Element of an Array with array_shift()
120
Slicing Arrays with array_slice() 121
Sorting Arrays 122
Sorting Numerically Indexed Arrays with sort() 122
Sorting an Associative Array by Value with asort() 123
Sorting an Associative Array by Key with ksort() 123
Summary 124
Q&A 124
Workshop 125
Quiz 125
Activities 125
Hour 8: Objects126
Overview 126
What Is an Object? 126
Creating an Object 127
Object Properties 128
Object Methods 129
An Example 132
Defining the Class's Properties 133
Creating a Constructor 133
The addRow() Method 134
The addRowAssocArray() Method 134
The output() Method 135
Bringing It All Together 136
What's Missing? 139
Why a Class? 139
Inheritance 140
Overriding the Method of a Parent Class 141
Calling an Overridden Method 143
Inheritance: An Example 144
Defining HTMLTable's Properties 144
Creating the Constructor 145
The setCellpadding() Method 146
The Output() Method 146
The Table and HTMLTable Classes in Their Entirety 147
Why Use Inheritance? 150
Summary 151
Q&A 152
Workshop 152
Quiz 152
Activities 153
Hour 9: Working with Forms 154
Overview 154
Global and Environment Variables 154
A Script to Acquire User Input 156
Accessing Input from Multiple SELECT Elements 157
Accessing All the Fields from a Form in an Associative Array
159
Distinguishing Between GET and POST Transactions 161
Combining HTML and PHP Code on a Single Page 162
Using Hidden Fields to Save State 165
Redirecting the User 167
File Upload Forms and Scripts 169
Summary 173
Q&A 173
Workshop 173
Quiz 174
Activities 174
Hour 10: Working with Files 175
Overview 175
Including Files with include() 175
Testing Files 178
Checking for Existence with file_exists() 179
A File or a Directory? 179
Checking the Status of a File 179
Determining File Size with filesize() 180
Getting Date Information About a File 180
Creating a Function That Performs Multiple File Tests 181
Creating and Deleting Files 183
Opening a File for Writing, Reading, or Appending 183
Reading from Files 184
Reading Lines from a File with fgets() and feof() 185
Reading Arbitrary Amounts of Data from a File with fread()
186
Reading Characters from a File with fgetc() 188
Writing or Appending to a File 189
Writing to a File with fwrite() or fputs() 189
Locking Files with flock() 190
Working with Directories 191
Creating Directories with mkdir() 191
Removing a Directory with rmdir() 192
Opening a Directory for Reading with opendir() 192
Reading the Contents of a Directory with readdir() 192
Summary 194
Q&A 194
Workshop 194
Quiz 194
Activities 195
Hour 11: Working with the DBM Functions 196
Overview 196
Opening a DBM Database 196
Adding Data to the Database 197
Amending Elements in a Database 198
Reading from a DBM Database 199
Determining Whether an Item Exists in a Database 201
Deleting an Item from a Database 201
Adding Complex Data Structures to a DBM Database 201
An Example 205
Summary 211
Q&A 211
Workshop 211
Quiz 211
Activities 212
Hour 12: Database Integration— MySQL 213
Overview 213
A (Very) Brief Introduction to SQL 213
Connecting to the Database Server 214
Selecting a Database 215
Finding Out About Errors 215
Adding Data to a Table 216
Acquiring the Value of an Automatically Incremented Field 220
Accessing Information 221
Finding the Number of Rows Found by a Query 221
Accessing a Resultset 222
Changing Data 225
Getting Information About Databases 227
Listing Databases 227
Listing the Tables Within a Database 229
Listing and Exploring Fields 229
Database Structure— Bringing It All Together 230
Summary 232
Q&A 233
Workshop 233
Quiz 234
Activities 234
Hour 13: Beyond the Box 235
Overview 235
Environmental Variables 235
A Brief Summary of an HTTP Client/Server Negotiation 238
The Request 238
The Response 241
Getting a Document from a Remote Address 243
Converting IP Addresses and Hostnames 244
Making a Network Connection 245
Making an NNTP Connection Using fsockopen() 249
Sending Mail with the mail() Function 252
Summary 253
Q&A 253
Workshop 254
Quiz 254
Activities 254
Hour 14: Working with Dynamic Images 256
Overview 256
Creating and Outputting Images 256
Acquiring Color 257
Drawing Lines 258
Applying Color Fills 259
Drawing an Arc 260
Drawing a Rectangle 261
Drawing a Polygon 262
Making a Color Transparent 263
Working with Text 264
Writing a String with imageTTFtext() 265
Testing Text Dimensions with imageTTFbox() 266
Bringing It Together 270
Summary 275
Q&A 275
Workshop 276
Quiz 276
Activities 276
Hour 15: Working with Dates 277
Overview 277
Getting the Date with time() 277
Converting a Time Stamp with getdate() 278
Converting a Time Stamp with date() 279
Creating Time Stamps with mktime() 282
Testing a Date with checkdate() 283
An Example 283
Checking User Input 284
Building the HTML Form 285
Creating the Calendar Table 287
Summary 292
Q&A 292
Workshop 292
Quiz 292
Activity 293
Hour 16: Working with Data 294
Overview 294
Data Types Revisited 294
A Recap 294
Converting Complex Types 295
Automatic Conversion of Data Types 297
Testing Data Types 298
More Ways of Changing Type 300
Why Are Data Types Important? 300
Testing for Absence and Emptiness 302
More About Arrays 303
An Alternative Approach to Traversing Arrays 304
Checking That a Value Exists in an Array 305
Removing an Element from an Array 306
Applying a Function to Every Element in an Array 306
Custom Sorting Arrays 307
Summary 311
Q&A 311
Workshop 311
Quiz 311
Activities 312
Hour 17: Working with Strings 313
Overview 313
Formatting Strings 313
Working with printf() 313
printf() and Type Specifiers 314
Padding Output with the Padding Specifier 317
Specifying a Field Width 318
Specifying Precision 319
Conversion Specifications: A Recap 320
Storing a Formatted String 322
Investigating Strings 323
A Note About Indexing Strings 323
Finding the Length of a String with strlen() 324
Finding a Substring Within a String with strstr() 324
Finding the Position of a Substring with strpos() 325
Extracting Part of a String with substr() 325
Tokenizing a String with strtok() 326
Manipulating Strings 328
Cleaning Up a String with trim() and ltrim() 328
Replacing a Portion of a String using substr_replace() 329
Replacing Substrings Using str_replace 329
Converting Case 329
Breaking Strings into Arrays with explode() 331
Summary 331
Q&A 332
Workshop 332
Quiz 332
Activities 333
Hour 18: Working with Regular Expressions 334
Overview 334
POSIX Regular Expression Functions 334
Using ereg() to Match Patterns in Strings 335
Using Quantifiers to Match a Character More Than Once
335
Matching Ranges of Characters with Character Classes 338
Working with Atoms 339
Branches 340
Anchoring a Regular Expression 340
The Membership Code Example Revisited 341
Using egrep_replace() to Replace Patterns in Strings 342
Using Back References with egrep_replace() 342
Using split() to Break Up Strings 343
Perl Compatible Regular Expressions (PCREs) 343
Matching Patterns with preg_match() 343

No comments:

Post a Comment