内容简介
1.Strings
1.1 Accessing Substrings
1.2 Extracting Substrings
1.3 Replacing Substrings
1.4 Processing a String One Byte at a Time
1.5 Reversing a String by Word or Byte
1.6 Generating a Random String
1.7 Expanding and Compressing Tabs
1.8 Controlling Case
1.9 Interpolating Functions and Expressions Within Strings
1.10 Trimming Blanks from a String
1.11 Generating Comma-Separated Data
1.12 Parsing Comma-Separated Data
1.13 Generating Fixed-Width Field Data Records
1.14 Parsing Fixed-Width Field Data Records
1.15 Taking Strings Apart
1.16 Wrapping Text at a Certain Line Length
1.17 Storing Binary Data in Strings
1.18 Program:Downloadable CSV File
2.Numbers
2.1 Checking Whether a Variable Contains a Valid Number
2.2 Comparing Floating-Point Numbers
2.3 Rounding Floating-Point Numbers
2.4 Operating on a Series of Integers
2.5 Generating Random Numbers Within a Range
2.6 Generating Predictable Random Numbers
2.7 Generating Biased Random Numbers
2.8 Taking Logarithms
2.9 Calculating Exponents
2.10 Formatting Numbers
2.11 Formatting Monetary Values
2.12 Printing Correct Plurals
2.13 Calculating Trigonometric Functions
2.14 Doing Trigonometry in Degrees, Not Radians
2.15 Handling Very Large or Very Small Numbers
2.16 Converting Between Bases
2.17 Calculating Using Numbers in Bases Other Than Decimal
2.18 Finding the Distance Between Two Places
3.Dates and Times
3.1 Finding the Current Date and Time
3.2 Converting Time and Date Parts to an Epoch Timestamp
3.3 Converting an Epoch Timestamp to Time and Date Parts
3.4 Printing a Date or Time in a Specified Format
3.5 Finding the Difference of Two Dates
3.6 Finding the Day in a Week,Month,or Year
3.7 Validating a Date
3.8 Parsing Dates and Times from Strings
3.9 Adding to or Subtracting from a Date
3.10 Calculating Time with Time Zones and Daylight Saving Time
3.11 Generating a High-Precision Time
3.12 Generating Time Ranges
3.13 Using Non-Gregorian Calendars
3.14 Program:Calendar
4.Arrays
4.1 Specifying an Array Not Beginning at Element 0
4.2 Storing Multiple Elements per Key in an Array
4.3 Initializing an Array to a Range of Integers
4.4 Iterating Through an Array
4.5 Deleting Elements from an Array
4.6 Changing Array Size
4.7 Appending One Array to Another
4.8 Turning an Array into a String
4.9 Printing an Array with Commas
4.10 Checking if a Key Is in an Array
4.11 Checking if an Element Is in an Array
4.12 Finding the Position of a Value in an Array
4.13 Finding Elements That Pass a Certain Test
4.14 Finding the Largest or Smallest Valued Element in an Array
4.15 Reversing an Array
4.16 Sorting an Array
4.17 Sorting an Array by a Computable Field
4.18 Sorting Multiple Arrays
4.19 Sorting an Array Using a Method Instead of a Function
4.20 Randomizing an Array
4.21 Removing Duplicate Elements from an Array
4.22 Applying a Function to Each Element in an Array
4.23 Finding the Union,Intersection,or Difference of Two Arrays
4.24 Iterating Efficiently over Large or Expensive Datasets
4.25 Accessing an Object Using Array Syntax
5.Variables
5.1 Avoiding==Versus=Confusion
5.2 Establishing a Default Value
5.3 Exchanging Values Without Using Temporary Variables
5.4 Creating a Dynamic Variable Name
5.5 Persisting a Local Variable's Value Across Function Invocations
5.6 Sharing Variables Between Processes
5.7 Encapsulating Complex Data Types in a String
5.8 Dumping Variable Contents as Strings
6.Functions
6.1 Accessing Function Parameters
6.2 Setting Default Values for Function Parameters
6.3 Passing Values by Reference
6.4 Using Named Parameters
6.5 Enforcing Types of Function Arguments
6.6 Creating Functions That Take a Variable Number of Arguments
6.7 Returning Values by Reference
6.8 Returning More Than One Value
6.9 Skipping Selected Return Values
6.10 Returning Failure
6.11 Calling Variable Functions
6.12 Accessing a Global Variable Inside a Function
6.13 Creating Dynamic Functions
7.Classes and Objects
7.1 Instantiating Objects
7.2 Defining Object Constructors
7.3 Defining Object Destructors
7.4 Implementing Access Control
7.5 Preventing Changes to Classes and Methods
7.6 Defining Object Stringification
7.7 Requiring Multiple Classes to Behave Similarly
7.8 Creating Abstract Base Classes
7.9 Assigning Object References
7.10 Cloning Objects
7.11 Overriding Property Accesses
7.12 Calling Methods on an Object Returned by Another Method
7.13 Aggregating Objects
7.14 Accessing Overridden Methods
7.15 Creating Methods Dynamically
7.16 Using Method Polymorphism
7.17 Defining Class Constants
7.18 Defining Static Properties and Methods
7.19 Controlling Object Serialization
7.20 Introspecting Objects
7.21 Checking If an Object Is an Instance of a Specific Class
7.22 Autoloading Class Files upon Object Instantiation
7.23 Instantiating an Object Dynamically
7.24 Program:whereis
8.Web Fundamentals
8.1 Setting Cookies
8.2 Reading Cookie Values
8.3 Deleting Cookies
8.4 Building a Query String
8.5 Reading the POST Request Body
8.6 Using HTTP Basic or Digest Authentication
8.7 Using Cookie Authentication
8.8 Reading an HTTP Header
8.9 Writing an HTTP Header
8.10 Sending a Specific HTTP Status Code
8.11 Redirecting to a Different Location
8.12 Flushing Output to the Browser
8.13 Buffering Output to the Browser
8.14 Compressing Web Output
8.15 Reading Environment Variables
8.16 Setting Environment Variables
8.17 Communicating Within Apache
8.18 Redirecting Mobile Browsers to a Mobile Optimized Site
8.19 Program:Website Account(De)activator
8.20 Program:Tiny Wiki
8.21 Program:HTTP Range
9.Forms
9.1 Processing Form Input
9.2 Validating Form Input:Required Fields
9.3 Validating Form Input:Numbers
9.4 Validating Form Input:Email Addresses
9.5 Validating Form Input:Drop-Down Menus
9.6 Validating Form Input:Radio Buttons
9.7 Validating Form Input:Checkboxes
9.8 Validating Form Input:Dates and Times
9.9 Validating Form Input:Credit Cards
9.10 Preventing Cross-Site Scripting
9.11 Processing Uploaded Files
9.12 Working with Multipage Forms
9.13 Redisplaying Forms with Inline Error Messages
9.14 Guarding Against Multiple Submissions of the Same Form
9.15 Preventing Global Variable Injection
9.16 Handling Remote Variables with Periods in Their Names
9.17 Using Form Elements with Multiple Options
9.18 Creating Drop-Down Menus Based on the Current Date
10.Database Access
10.1 Using DBM Databases
10.2 Using an SQLite Database
10.3 Connecting to an SQL Database
10.4 Querying an SQL Database
10.5 Retrieving Rows Without a Loop
10.6 Modifying Data in an SQL Database
10.7 Repeating Queries Efficiently
10.8 Finding the Number of Rows Returned by a Query
10.9 Escaping Quotes
10.10 Logging Debugging Information and Errors
10.11 Creating Unique Identifiers
10.12 Building Queries Programmatically
10.13 Making Paginated Links for a Series of Records
10.14 Caching Queries and Results
10.15 Accessing a Database Connection Anywhere in Your Program
10.16 Program:Storing a Threaded Message Board
10.17 Using Redis
11.Sessions and Data Persistence
11.1 Using Session Tracking
11.2 Preventing Session Hijacking
11.3 Preventing Session Fixation
11.4 Storing Sessons in Memcached
11.5 Storing Sessions in a Database
11.6 Storing Arbitrary Data in Shared Memory
11.7 Caching Calculated Results in Summary Tables
12.XML
12.1 Generating XML as a String
12.2 Generating XML with DOM
12.3 Parsing Basic XML Documents
12.4 Parsing Complex XML Documents
12.5 Parsing Large XML Documents
12.6 Extracting Information Using XPath
12.7 Transforming XML with XSLT
12.8 Setting XSLT Parameters from PHP
12.9 Calling PHP Functions from XSLT Stylesheets
12.10 Validating XML Documents
12.11 Handling Content Encoding
12.12 Reading RSS and Atom Feeds
12.13 Writing RSS Feeds
12.14 Writing Atom Feeds
13.Web Automation
13.1 Marking Up a Web Page
13.2 Cleaning Up Broken or Nonstandard HTML
13.3 Extracting Links from an HTML File
13.4 Converting Plain Text to HTML
13.5 Converting HTML to Plain Text
13.6 Removing HTML and PHP Tags
13.7 Responding to an Ajax Request
13.8 Integrating with JavaScript
13.9 Program:Finding Stale Links
13.10 Program:Finding Fresh Links
14.Consuming RESTfulAPIs
14.1 Fetching a URL with the GET Method
14.2 Fetching a URL with the POST Method and Form Data
14.3 Fetching a URL with an Arbitrary Method and POST Body
14.4 Fetching a URL with Cookies
14.5 Fetching a URL with Arbitrary Headers
14.6 Fetching a URL with a Timeout
14.7 Fetching an HTTPS URL
14.8 Debugging the Raw HTTP Exchange
14.9 Making an OAuth 1.0 Request
14.10 Making an OAuth 2.0 Request
15.Serving RESTful APIs
15.1 Exposing and Routing to a Resource
15.2 Exposing Clean Resource Paths
15.3 Exposing a Resource for Reading
15.4 Creating a Resource
15.5 Editing a Resource
15.6 Deleting a Resource
15.7 Indicating Errors and Failures
15.8 Supporting Multiple Formats
16.Internet Services
16.1 Sending Mail
16.2 Sending MIME Mail
16.3 Reading Mail with IMAP or POP3
16.4 Getting and Putting Files with FTP
16.5 Looking Up Addresses with LDAP
16.6 Using LDAP for User Authentication
16.7 Performing DNS Lookups
16.8 Checking If a Host Is Alive
16.9 Getting Information About a Domain Name
17.Graphics
17.1 Drawing Lines,Rectangles,and Polygons
17.2 Drawing Arcs,Ellipses,and Circles
17.3 Drawing with Patterned Lines
17.4 Drawing Text
17.5 Drawing Centered Text
17.6 Building Dynamic Images
17.7 Getting and Setting a Transparent Color
17.8 Overlaying Watermarks
17.9 Creating Thumbnail Images
17.10 Reading EXIF Data
17.11 Serving Images Securely
17.12 Program:Generating Bar Charts from Poll Results
18.Security and Encryption
18.1 Preventing Session Fixation
18.2 Protecting Against Form Spoofing
18.3 Ensuring Input Is Filtered
18.4 Avoiding Cross-Site Scripting
18.5 Eliminating SQL Injection
18.6 Keeping Passwords Out of Your Site Files
18.7 Storing Passwords
18.8 Dealing with Lost Passwords
18.9 Verifying Data with Hashes
18.10 Encrypting and Decrypting Data
18.11 Storing Encrypted Data in a File or Database
18.12 Sharing Encrypted Data with Another Website
18.13 Detecting SSL
18.14 Encrypting Email with GPG
19.Internationalization and Localization
19.1 Determining the User's Locale
19.2 Localizing Text Messages
19.3 Localizing Dates and Times
19.4 Localizing Numbers
19.5 Localizing Currency Values
19.6 Localizing Images
19.7 Localizing Included Files
19.8 Sorting in a Locale-Aware Order
19.9 Managing Localization Resources
19.10 Setting the Character Encoding of Outgoing Data
19.11 Setting the Character Encoding of Incoming Data
19.12 Manipulating UTF-8 Text
20.Error Handling
20.1 Finding and Fixing Parse Errors
20.2 Creating Your Own Exception Classes
20.3 Printing a Stack Trace
20.4 Reading Configuration Variables
20.5 Setting Configuration Variables
20.6 Hiding Error Messages from Users
20.7 Tuning Error Handling
20.8 Using a Custom Error Handler
20.9 Logging Errors
20.10 Eliminating"headers already sent"Errors
20.11 Logging Debugging Information
21.Software Engineering
21.1 Using a Debugger Extension
21.2 Writing a Unit Test
21.3 Writing a Unit Test Suite
21.4 Applying a Unit Test to a Web Page
21.5 Setting Up a Test Environment
21.6 Using the Built-in Web Server
22.Performance Tuning
22.1 Using an Accelerator
22.2 Timing Function Execution
22.3 Timing Program Execution by Function
22.4 Timing Program Execution by Statement
22.5 Timing Program Execution by Section
22.6 Profiling with a Debugger Extension
22.7 Stress-Testing Your Website
22.8 Avoiding Regular Expressions
23.Regular Expressions
23.1 Switching from ereg to preg
23.2 Matching Words
23.3 Finding the nth Occurrence of a Match
23.4 Choosing Greedy or Nongreedy Matches
23.5 Finding All Lines in a File That Match a Pattern
23.6 Capturing Text Inside HTML Tags
23.7 Preventing Parentheses from Capturing Text
23.8 Escaping Special Characters in a Regular Expression
23.9 Reading Records with a Pattern Separator
23.10 Using a PHP Function in a Regular Expression
24.Files
24.1 Creating or Opening a Local File
24.2 Creating a Temporary File
24.3 Opening a Remote File
24.4 Reading from Standard Input
24.5 Reading a File into a String
24.6 Counting Lines,Paragraphs,or Records in a File
24.7 Processing Every Word in a File
24.8 Picking a Random Line from a File
24.9 Randomizing All Lines in a File
24.10 Processing Variable-Length Text Fields
24.11 Reading Configuration Files
24.12 Modifying a File in Place Without a Temporary File
24.13 Flushing Output to a File
24.14 Writing to Standard Output
24.15 Writing to Many Filehandles Simultaneously
24.16 Escaping Shell Metacharacters
24.17 Passing Input to a Program
24.18 Reading Standard Output from a Program
24.19 Reading Standard Error from a Program
24.20 Locking a File
24.21 Reading and Writing Custom File Types
24.22 Reading and Writing Compressed Files
25.Directories
25.1 Getting and Setting File Timestamps
25.2 Getting File Information
25.3 Changing File Permissions or Ownership
25.4 Splitting a Filename into Its Component Parts
25.5 Deleting a File
25.6 Copying or Moving a File
25.7 Processing All Files in a Directory
25.8 Getting a List of Filenames Matching a Pattern
25.9 Processing All Files in a Directory Recursively
25.10 Making New Directories
25.11 Removing a Directory and Its Contents
25.12 Program:Web Server Directory Listing
25.13 Program:Site Search
26.Command-Line PHP
26.1 Parsing Program Arguments
26.2 Parsing Program Arguments with getopt
26.3 Reading from the Keyboard
26.4 Running PHP Code on Every Line of an Input File
26.5 Reading Passwords
26.6 Colorizing Console Output
26.7 Program:DOM Explorer
27.Packages
27.1 Defining and Installing Composer Dependencies
27.2 Finding Composer Packages
27.3 Installing Composer Packages
27.4 Using the PEAR Installer
27.5 Finding PEAR Packages
27.6 Finding Information About a Package
27.7 Installing PEAR Packages
27.8 Upgrading PEAR Packages
27.9 Uninstalling PEAR Packages
27.10 Installing PECL Packages
Index