1.Strings 1
1.1 Accessing Substrings 5
1.2 Extracting Substrings 6
1.3 Replacing Substrings 7
1.4 Processing a String One Byte at a Time 9
1.5 Reversing a String by Word or Byte 10
1.6 Generating a Random String 11
1.7 Expanding and Compressing Tabs 12
1.8 Controlling Case 14
1.9 Interpolating Functions and Expressions Within Strings 16
1.10 Trimming Blanks from a String 17
1.11 Generating Comma-Separated Data 19
1.12 Parsing Comma-Separated Data 20
1.13 Generating Fixed-Width Field Data Records 21
1.14 Parsing Fixed-Width Field Data Records 22
1.15 Taking Strings Apart 25
1.16 Wrapping Text at a Certain Line Length 27
1.17 Storing Binary Data in Strings 29
1.18 Program:Downloadable CSV File 31
2.Numbers 35
2.1 Checking Whether a Variable Contains a Valid Number 36
2.2 Comparing Floating-Point Numbers 37
2.3 Rounding Floating-Point Numbers 38
2.4 Operating on a Series of Integers 40
2.5 Generating Random Numbers Within a Range 42
2.6 Generating Predictable Random Numbers 43
2.7 Generating Biased Random Numbers 44
2.8 Taking Logarithms 46
2.9 Calculating Exponents 46
2.10 Formatting Numbers 47
2.11 Formatting Monetary Values 49
2.12 Printing Correct Plurals 50
2.13 Calculating Trigonometric Functions 51
2.14 Doing Trigonometry in Degrees, Not Radians 52
2.15 Handling Very Large or Very Small Numbers 53
2.16 Converting Between Bases 55
2.17 Calculating Using Numbers in Bases Other Than Decimal 56
2.18 Finding the Distance Between Two Places 58
3.Dates and Times 61
3.1 Finding the Current Date and Time 63
3.2 Converting Time and Date Parts to an Epoch Timestamp 66
3.3 Converting an Epoch Timestamp to Time and Date Parts 68
3.4 Printing a Date or Time in a Specified Format 69
3.5 Finding the Difference of Two Dates 72
3.6 Finding the Day in a Week,Month,or Year 74
3.7 Validating a Date 76
3.8 Parsing Dates and Times from Strings 77
3.9 Adding to or Subtracting from a Date 80
3.10 Calculating Time with Time Zones and Daylight Saving Time 81
3.11 Generating a High-Precision Time 83
3.12 Generating Time Ranges 84
3.13 Using Non-Gregorian Calendars 85
3.14 Program:Calendar 88
4.Arrays 95
4.1 Specifying an Array Not Beginning at Element 0 98
4.2 Storing Multiple Elements per Key in an Array 99
4.3 Initializing an Array to a Range of Integers 101
4.4 Iterating Through an Array 101
4.5 Deleting Elements from an Array 104
4.6 Changing Array Size 106
4.7 Appending One Array to Another 108
4.8 Turning an Array into a String 110
4.9 Printing an Array with Commas 111
4.10 Checking if a Key Is in an Array 113
4.11 Checking if an Element Is in an Array 113
4.12 Finding the Position of a Value in an Array 115
4.13 Finding Elements That Pass a Certain Test 116
4.14 Finding the Largest or Smallest Valued Element in an Array 117
4.15 Reversing an Array 118
4.16 Sorting an Array 119
4.17 Sorting an Array by a Computable Field 120
4.18 Sorting Multiple Arrays 123
4.19 Sorting an Array Using a Method Instead of a Function 124
4.20 Randomizing an Array 125
4.21 Removing Duplicate Elements from an Array 126
4.22 Applying a Function to Each Element in an Array 127
4.23 Finding the Union,Intersection,or Difference of Two Arrays 129
4.24 Iterating Efficiently over Large or Expensive Datasets 131
4.25 Accessing an Object Using Array Syntax 133
5.Variables 137
5.1 Avoiding==Versus=Confusion 139
5.2 Establishing a Default Value 140
5.3 Exchanging Values Without Using Temporary Variables 141
5.4 Creating a Dynamic Variable Name 142
5.5 Persisting a Local Variable's Value Across Function Invocations 143
5.6 Sharing Variables Between Processes 145
5.7 Encapsulating Complex Data Types in a String 152
5.8 Dumping Variable Contents as Strings 153
6.Functions 159
6.1 Accessing Function Parameters 160
6.2 Setting Default Values for Function Parameters 161
6.3 Passing Values by Reference 163
6.4 Using Named Parameters 164
6.5 Enforcing Types of Function Arguments 165
6.6 Creating Functions That Take a Variable Number of Arguments 166
6.7 Returning Values by Reference 169
6.8 Returning More Than One Value 171
6.9 Skipping Selected Return Values 172
6.10 Returning Failure 173
6.11 Calling Variable Functions 174
6.12 Accessing a Global Variable Inside a Function 177
6.13 Creating Dynamic Functions 178
7.Classes and Objects 181
7.1 Instantiating Objects 185
7.2 Defining Object Constructors 186
7.3 Defining Object Destructors 187
7.4 Implementing Access Control 188
7.5 Preventing Changes to Classes and Methods 191
7.6 Defining Object Stringification 192
7.7 Requiring Multiple Classes to Behave Similarly 193
7.8 Creating Abstract Base Classes 197
7.9 Assigning Object References 199
7.10 Cloning Objects 200
7.11 Overriding Property Accesses 203
7.12 Calling Methods on an Object Returned by Another Method 207
7.13 Aggregating Objects 208
7.14 Accessing Overridden Methods 212
7.15 Creating Methods Dynamically 214
7.16 Using Method Polymorphism 215
7.17 Defining Class Constants 217
7.18 Defining Static Properties and Methods 219
7.19 Controlling Object Serialization 222
7.20 Introspecting Objects 224
7.21 Checking If an Object Is an Instance of a Specific Class 228
7.22 Autoloading Class Files upon Object Instantiation 231
7.23 Instantiating an Object Dynamically 232
7.24 Program:whereis 233
8.Web Fundamentals 237
8.1 Setting Cookies 238
8.2 Reading Cookie Values 240
8.3 Deleting Cookies 240
8.4 Building a Query String 241
8.5 Reading the POST Request Body 242
8.6 Using HTTP Basic or Digest Authentication 243
8.7 Using Cookie Authentication 247
8.8 Reading an HTTP Header 250
8.9 Writing an HTTP Header 251
8.10 Sending a Specific HTTP Status Code 252
8.11 Redirecting to a Different Location 253
8.12 Flushing Output to the Browser 255
8.13 Buffering Output to the Browser 255
8.14 Compressing Web Output 257
8.15 Reading Environment Variables 258
8.16 Setting Environment Variables 258
8.17 Communicating Within Apache 259
8.18 Redirecting Mobile Browsers to a Mobile Optimized Site 260
8.19 Program:Website Account(De)activator 261
8.20 Program:Tiny Wiki 264
8.21 Program:HTTP Range 267
9.Forms 277
9.1 Processing Form Input 279
9.2 Validating Form Input:Required Fields 281
9.3 Validating Form Input:Numbers 283
9.4 Validating Form Input:Email Addresses 285
9.5 Validating Form Input:Drop-Down Menus 286
9.6 Validating Form Input:Radio Buttons 287
9.7 Validating Form Input:Checkboxes 289
9.8 Validating Form Input:Dates and Times 291
9.9 Validating Form Input:Credit Cards 292
9.10 Preventing Cross-Site Scripting 293
9.11 Processing Uploaded Files 294
9.12 Working with Multipage Forms 297
9.13 Redisplaying Forms with Inline Error Messages 299
9.14 Guarding Against Multiple Submissions of the Same Form 301
9.15 Preventing Global Variable Injection 303
9.16 Handling Remote Variables with Periods in Their Names 305
9.17 Using Form Elements with Multiple Options 306
9.18 Creating Drop-Down Menus Based on the Current Date 307
10.Database Access 309
10.1 Using DBM Databases 312
10.2 Using an SQLite Database 315
10.3 Connecting to an SQL Database 317
10.4 Querying an SQL Database 318
10.5 Retrieving Rows Without a Loop 321
10.6 Modifying Data in an SQL Database 322
10.7 Repeating Queries Efficiently 323
10.8 Finding the Number of Rows Returned by a Query 326
10.9 Escaping Quotes 327
10.10 Logging Debugging Information and Errors 329
10.11 Creating Unique Identifiers 331
10.12 Building Queries Programmatically 333
10.13 Making Paginated Links for a Series of Records 338
10.14 Caching Queries and Results 341
10.15 Accessing a Database Connection Anywhere in Your Program 344
10.16 Program:Storing a Threaded Message Board 346
10.17 Using Redis 353
11.Sessions and Data Persistence 355
11.1 Using Session Tracking 356
11.2 Preventing Session Hijacking 358
11.3 Preventing Session Fixation 359
11.4 Storing Sessons in Memcached 360
11.5 Storing Sessions in a Database 361
11.6 Storing Arbitrary Data in Shared Memory 364
11.7 Caching Calculated Results in Summary Tables 367
12.XML 371
12.1 Generating XML as a String 374
12.2 Generating XML with DOM 375
12.3 Parsing Basic XML Documents 378
12.4 Parsing Complex XML Documents 381
12.5 Parsing Large XML Documents 383
12.6 Extracting Information Using XPath 389
12.7 Transforming XML with XSLT 392
12.8 Setting XSLT Parameters from PHP 394
12.9 Calling PHP Functions from XSLT Stylesheets 396
12.10 Validating XML Documents 400
12.11 Handling Content Encoding 402
12.12 Reading RSS and Atom Feeds 403
12.13 Writing RSS Feeds 406
12.14 Writing Atom Feeds 409
13.Web Automation 415
13.1 Marking Up a Web Page 416
13.2 Cleaning Up Broken or Nonstandard HTML 419
13.3 Extracting Links from an HTML File 422
13.4 Converting Plain Text to HTML 425
13.5 Converting HTML to Plain Text 426
13.6 Removing HTML and PHP Tags 426
13.7 Responding to an Ajax Request 430
13.8 Integrating with JavaScript 432
13.9 Program:Finding Stale Links 435
13.10 Program:Finding Fresh Links 438
14.Consuming RESTfulAPIs 441
14.1 Fetching a URL with the GET Method 442
14.2 Fetching a URL with the POST Method and Form Data 446
14.3 Fetching a URL with an Arbitrary Method and POST Body 448
14.4 Fetching a URL with Cookies 450
14.5 Fetching a URL with Arbitrary Headers 452
14.6 Fetching a URL with a Timeout 454
14.7 Fetching an HTTPS URL 456
14.8 Debugging the Raw HTTP Exchange 456
14.9 Making an OAuth 1.0 Request 461
14.10 Making an OAuth 2.0 Request 462
15.Serving RESTful APIs 467
15.1 Exposing and Routing to a Resource 470
15.2 Exposing Clean Resource Paths 473
15.3 Exposing a Resource for Reading 474
15.4 Creating a Resource 476
15.5 Editing a Resource 481
15.6 Deleting a Resource 483
15.7 Indicating Errors and Failures 484
15.8 Supporting Multiple Formats 486
16.Internet Services 491
16.1 Sending Mail 492
16.2 Sending MIME Mail 494
16.3 Reading Mail with IMAP or POP3 496
16.4 Getting and Putting Files with FTP 500
16.5 Looking Up Addresses with LDAP 502
16.6 Using LDAP for User Authentication 504
16.7 Performing DNS Lookups 506
16.8 Checking If a Host Is Alive 509
16.9 Getting Information About a Domain Name 510
17.Graphics 513
17.1 Drawing Lines,Rectangles,and Polygons 516
17.2 Drawing Arcs,Ellipses,and Circles 519
17.3 Drawing with Patterned Lines 521
17.4 Drawing Text 522
17.5 Drawing Centered Text 525
17.6 Building Dynamic Images 528
17.7 Getting and Setting a Transparent Color 531
17.8 Overlaying Watermarks 532
17.9 Creating Thumbnail Images 534
17.10 Reading EXIF Data 538
17.11 Serving Images Securely 539
17.12 Program:Generating Bar Charts from Poll Results 541
18.Security and Encryption 545
18.1 Preventing Session Fixation 546
18.2 Protecting Against Form Spoofing 547
18.3 Ensuring Input Is Filtered 548
18.4 Avoiding Cross-Site Scripting 549
18.5 Eliminating SQL Injection 550
18.6 Keeping Passwords Out of Your Site Files 551
18.7 Storing Passwords 552
18.8 Dealing with Lost Passwords 555
18.9 Verifying Data with Hashes 557
18.10 Encrypting and Decrypting Data 559
18.11 Storing Encrypted Data in a File or Database 561
18.12 Sharing Encrypted Data with Another Website 564
18.13 Detecting SSL 566
18.14 Encrypting Email with GPG 567
19.Internationalization and Localization 571
19.1 Determining the User's Locale 573
19.2 Localizing Text Messages 574
19.3 Localizing Dates and Times 577
19.4 Localizing Numbers 581
19.5 Localizing Currency Values 584
19.6 Localizing Images 585
19.7 Localizing Included Files 587
19.8 Sorting in a Locale-Aware Order 588
19.9 Managing Localization Resources 589
19.10 Setting the Character Encoding of Outgoing Data 591
19.11 Setting the Character Encoding of Incoming Data 592
19.12 Manipulating UTF-8 Text 593
20.Error Handling 599
20.1 Finding and Fixing Parse Errors 600
20.2 Creating Your Own Exception Classes 602
20.3 Printing a Stack Trace 605
20.4 Reading Configuration Variables 608
20.5 Setting Configuration Variables 610
20.6 Hiding Error Messages from Users 610
20.7 Tuning Error Handling 612
20.8 Using a Custom Error Handler 614
20.9 Logging Errors 615
20.10 Eliminating"headers already sent"Errors 617
20.11 Logging Debugging Information 618
21.Software Engineering 621
21.1 Using a Debugger Extension 621
21.2 Writing a Unit Test 625
21.3 Writing a Unit Test Suite 626
21.4 Applying a Unit Test to a Web Page 628
21.5 Setting Up a Test Environment 630
21.6 Using the Built-in Web Server 631
22.Performance Tuning 635
22.1 Using an Accelerator 636
22.2 Timing Function Execution 637
22.3 Timing Program Execution by Function 638
22.4 Timing Program Execution by Statement 640
22.5 Timing Program Execution by Section 642
22.6 Profiling with a Debugger Extension 644
22.7 Stress-Testing Your Website 648
22.8 Avoiding Regular Expressions 649
23.Regular Expressions 653
23.1 Switching from ereg to preg 657
23.2 Matching Words 659
23.3 Finding the nth Occurrence of a Match 660
23.4 Choosing Greedy or Nongreedy Matches 662
23.5 Finding All Lines in a File That Match a Pattern 664
23.6 Capturing Text Inside HTML Tags 665
23.7 Preventing Parentheses from Capturing Text 666
23.8 Escaping Special Characters in a Regular Expression 668
23.9 Reading Records with a Pattern Separator 669
23.10 Using a PHP Function in a Regular Expression 670
24.Files 673
24.1 Creating or Opening a Local File 677
24.2 Creating a Temporary File 678
24.3 Opening a Remote File 679
24.4 Reading from Standard Input 680
24.5 Reading a File into a String 681
24.6 Counting Lines,Paragraphs,or Records in a File 682
24.7 Processing Every Word in a File 685
24.8 Picking a Random Line from a File 687
24.9 Randomizing All Lines in a File 687
24.10 Processing Variable-Length Text Fields 688
24.11 Reading Configuration Files 690
24.12 Modifying a File in Place Without a Temporary File 692
24.13 Flushing Output to a File 693
24.14 Writing to Standard Output 694
24.15 Writing to Many Filehandles Simultaneously 695
24.16 Escaping Shell Metacharacters 696
24.17 Passing Input to a Program 697
24.18 Reading Standard Output from a Program 698
24.19 Reading Standard Error from a Program 700
24.20 Locking a File 701
24.21 Reading and Writing Custom File Types 703
24.22 Reading and Writing Compressed Files 708
25.Directories 711
25.1 Getting and Setting File Timestamps 714
25.2 Getting File Information 715
25.3 Changing File Permissions or Ownership 716
25.4 Splitting a Filename into Its Component Parts 717
25.5 Deleting a File 719
25.6 Copying or Moving a File 719
25.7 Processing All Files in a Directory 720
25.8 Getting a List of Filenames Matching a Pattern 721
25.9 Processing All Files in a Directory Recursively 723
25.10 Making New Directories 723
25.11 Removing a Directory and Its Contents 724
25.12 Program:Web Server Directory Listing 725
25.13 Program:Site Search 729
26.Command-Line PHP 733
26.1 Parsing Program Arguments 735
26.2 Parsing Program Arguments with getopt 736
26.3 Reading from the Keyboard 738
26.4 Running PHP Code on Every Line of an Input File 740
26.5 Reading Passwords 742
26.6 Colorizing Console Output 744
26.7 Program:DOM Explorer 746
27.Packages 751
27.1 Defining and Installing Composer Dependencies 754
27.2 Finding Composer Packages 755
27.3 Installing Composer Packages 757
27.4 Using the PEAR Installer 760
27.5 Finding PEAR Packages 763
27.6 Finding Information About a Package 765
27.7 Installing PEAR Packages 766
27.8 Upgrading PEAR Packages 768
27.9 Uninstalling PEAR Packages 769
27.10 Installing PECL Packages 770
Index 773