Book description
When it comes to creating dynamic web sites, the open source PHP language is red-hot property: used on more than 20 million web sites today, PHP is now more popular than Microsoft's ASP.NET technology. With our Cookbook's unique format, you can learn how to build dynamic web applications that work on any web browser. This revised new edition makes it easy to find specific solutions for programming challenges.
PHP Cookbook has a wealth of solutions for problems that you'll face regularly. With topics that range from beginner questions to advanced web programming techniques, this guide contains practical examples -- or "recipes" -- for anyone who uses this scripting language to generate dynamic web content. Updated for PHP 5, this book provides solutions that explain how to use the new language features in detail, including the vastly improved object-oriented capabilities and the new PDO data access extension. New sections on classes and objects are included, along with new material on processing XML, building web services with PHP, and working with SOAP/REST architectures. With each recipe, the authors include a discussion that explains the logic and concepts underlying the solution.
Publisher resources
Table of contents
- A Note Regarding Supplemental Files
- Preface
-
1. Strings
- 1.0. Introduction
- 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. Expanding and Compressing Tabs
- 1.7. Controlling Case
- 1.8. Interpolating Functions and Expressions Within Strings
- 1.9. Trimming Blanks from a String
- 1.10. Generating Comma-Separated Data
- 1.11. Parsing Comma-Separated Data
- 1.12. Generating Fixed-Width Field Data Records
- 1.13. Parsing Fixed-Width Field Data Records
- 1.14. Taking Strings Apart
- 1.15. Wrapping Text at a Certain Line Length
- 1.16. Storing Binary Data in Strings
- 1.17. Program: Downloadable CSV File
-
2. Numbers
- 2.0. Introduction
- 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 Biased Random Numbers
- 2.7. Taking Logarithms
- 2.8. Calculating Exponents
- 2.9. Formatting Numbers
- 2.10. Formatting Monetary Values
- 2.11. Printing Correct Plurals
- 2.12. Calculating Trigonometric Functions
- 2.13. Doing Trigonometry in Degrees, Not Radians
- 2.14. Handling Very Large or Very Small Numbers
- 2.15. Converting Between Bases
- 2.16. Calculating Using Numbers in Bases Other Than Decimal
- 2.17. Finding the Distance Between Two Places
-
3. Dates and Times
- 3.0. Introduction
- 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 Difference of Two Dates with Julian Days
- 3.7. Finding the Day in a Week, Month, or Year
- 3.8. Validating a Date
- 3.9. Parsing Dates and Times from Strings
- 3.10. Adding to or Subtracting from a Date
- 3.11. Calculating Time with Time Zones
- 3.12. Accounting for Daylight Savings Time
- 3.13. Generating a High-Precision Time
- 3.14. Generating Time Ranges
- 3.15. Using Non-Gregorian Calendars
- 3.16. Using Dates Outside the Range of an Epoch Timestamp
- 3.17. Program: Calendar
-
4. Arrays
- 4.0. Introduction
- 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. Making an Object Act like an Array
- 4.25. Program: Printing a Horizontally Columned HTML Table
-
5. Variables
- 5.0. Introduction
- 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. Using Static Variables
- 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.0. Introduction
- 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. Creating Functions That Take a Variable Number of Arguments
- 6.6. Returning Values by Reference
- 6.7. Returning More Than One Value
- 6.8. Skipping Selected Return Values
- 6.9. Returning Failure
- 6.10. Calling Variable Functions
- 6.11. Accessing a Global Variable Inside a Function
- 6.12. Creating Dynamic Functions
-
7. Classes and Objects
- 7.0. Introduction
- 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. Specifying Interfaces
- 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. Using Method Polymorphism
- 7.16. Defining Class Constants
- 7.17. Defining Static Properties and Methods
- 7.18. Controlling Object Serialization
- 7.19. Introspecting Objects
- 7.20. Checking if an Object Is an Instance of a Specific Class
- 7.21. Autoloading Class Files upon Object Instantiation
- 7.22. Instantiating an Object Dynamically
- 7.23. Program: whereis
-
8. Web Basics
- 8.0. Introduction
- 8.1. Setting Cookies
- 8.2. Reading Cookie Values
- 8.3. Deleting Cookies
- 8.4. Redirecting to a Different Location
- 8.5. Detecting Different Browsers
- 8.6. Building a Query String
- 8.7. Reading the Post Request Body
- 8.8. Generating HTML Tables with Alternating Row Styles
- 8.9. Using HTTP Basic or Digest Authentication
- 8.10. Using Cookie Authentication
- 8.11. Flushing Output to the Browser
- 8.12. Buffering Output to the Browser
- 8.13. Compressing Web Output
- 8.14. Reading Environment Variables
- 8.15. Setting Environment Variables
- 8.16. Communicating Within Apache
- 8.17. Program: Web Site Account (De)activator
- 8.18. Program: Tiny Wiki
-
9. Form
- 9.0. Introduction
- 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. Working with Multipage Forms
- 9.12. Redisplaying Forms with Inline Error Messages
- 9.13. Guarding Against Multiple Submission of the Same Form
- 9.14. Processing Uploaded Files
- 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.0. Introduction
- 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
- 11. Sessions and Data Persistence
-
12. XML
- 12.0. Introduction
- 12.1. Generating XML as a String
- 12.2. Generating XML with the 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.0. Introduction
- 13.1. Fetching a URL with the Get Method
- 13.2. Fetching a URL with the Post Method
- 13.3. Fetching a URL with Cookies
- 13.4. Fetching a URL with Arbitrary Headers
- 13.5. Fetching a URL with an Arbitrary Method
- 13.6. Fetching a URL with a Timeout
- 13.7. Fetching an HTTPS URL
- 13.8. Debugging the Raw HTTP Exchange
- 13.9. Marking Up a Web Page
- 13.10. Cleaning Up Broken or Nonstandard HTML
- 13.11. Extracting Links from an HTML File
- 13.12. Converting Plain Text to HTML
- 13.13. Converting HTML to Plain Text
- 13.14. Removing HTML and PHP Tags
- 13.15. Responding to an Ajax Request
- 13.16. Integrating with JavaScript
- 13.17. Program: Finding Stale Links
- 13.18. Program: Finding Fresh Links
-
14. Consuming Web Services
- 14.0. Introduction
- 14.1. Calling a REST Method
- 14.2. Calling a SOAP Method with WSDL
- 14.3. Calling a SOAP Method Without WSDL
- 14.4. Debugging SOAP Requests
- 14.5. Using Complex SOAP Types
- 14.6. Setting SOAP Types
- 14.7. Using SOAP Headers
- 14.8. Using Authentication with SOAP
- 14.9. Redefining an Endpoint
- 14.10. Catching SOAP Faults
- 14.11. Mapping XML Schema Data Types to PHP Classes
- 14.12. Calling an XML-RPC Method
- 14.13. Using Authentication with XML-RPC
-
15. Building Web Services
- 15.0. Introduction
- 15.1. Serving a REST Method
- 15.2. Serving a SOAP Method
- 15.3. Accepting Arguments in a SOAP Method
- 15.4. Generating WSDL Automatically
- 15.5. Throwing SOAP Faults
- 15.6. Processing a SOAP Header
- 15.7. Generating a SOAP Header
- 15.8. Using Authentication with SOAP
- 15.9. Serving an XML-RPC Method
-
16. Internet Services
- 16.0. Introduction
- 16.1. Sending Mail
- 16.2. Sending MIME Mail
- 16.3. Reading Mail with IMAP or POP3
- 16.4. Posting Messages to Usenet Newsgroups
- 16.5. Reading Usenet News Messages
- 16.6. Getting and Putting Files with FTP
- 16.7. Looking Up Addresses with LDAP
- 16.8. Using LDAP for User Authentication
- 16.9. Performing DNS Lookups
- 16.10. Checking if a Host Is Alive
- 16.11. Getting Information About a Domain Name
-
17. Graphics
- 17.0. Introduction
- 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. Reading EXIF Data
- 17.9. Serving Images Securely
- 17.10. Program: Generating Bar Charts from Poll Results
-
18. Security and Encryption
- 18.0. Introduction
- 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 Web Site
- 18.13. Detecting SSL
- 18.14. Encrypting Email with GPG
-
19. Internationalization and Localization
- 19.0. Introduction
- 19.1. Listing Available Locales
- 19.2. Using a Particular Locale
- 19.3. Setting the Default Locale
- 19.4. Localizing Text Messages
- 19.5. Localizing Dates and Times
- 19.6. Localizing Currency Values
- 19.7. Localizing Images
- 19.8. Localizing Included Files
- 19.9. Managing Localization Resources
- 19.10. Using gettext
- 19.11. Setting the Character Encoding of Outgoing Data
- 19.12. Setting the Character Encoding of Incoming Data
- 19.13. Manipulating UTF-8 Text
-
20. Error Handling, Debugging, andTesting
- 20.0. Introduction
- 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
- 20.12. Using a Debugger Extension
- 20.13. Writing a Unit Test
- 20.14. Writing a Unit Test Suite
- 20.15. Applying a Unit Test to a Web Page
- 20.16. Setting Up a Test Environment
- 21. Performance Tuning and Load Testing
-
22. Regular Expressions
- 22.0. Introduction
- 22.1. Switching from ereg to preg
- 22.2. Matching Words
- 22.3. Finding the nth Occurrence of a Match
- 22.4. Choosing Greedy or Nongreedy Matches
- 22.5. Finding All Lines in a File That Match a Pattern
- 22.6. Capturing Text Inside HTML Tags
- 22.7. Preventing Parentheses from Capturing Text
- 22.8. Escaping Special Characters in a Regular Expression
- 22.9. Reading Records with a Pattern Separator
- 22.10. Using a PHP Function in a Regular Expression
-
23. Files
- 23.0. Introduction
- 23.1. Creating or Opening a Local File
- 23.2. Creating a Temporary File
- 23.3. Opening a Remote File
- 23.4. Reading from Standard Input
- 23.5. Reading a File into a String
- 23.6. Counting Lines, Paragraphs, or Records in a File
- 23.7. Processing Every Word in a File
- 23.8. Picking a Random Line from a File
- 23.9. Randomizing All Lines in a File
- 23.10. Processing Variable-Length Text Fields
- 23.11. Reading Configuration Files
- 23.12. Modifying a File in Place Without a Temporary File
- 23.13. Flushing Output to a File
- 23.14. Writing to Standard Output
- 23.15. Writing to Many Filehandles Simultaneously
- 23.16. Escaping Shell Metacharacters
- 23.17. Passing Input to a Program
- 23.18. Reading Standard Output from a Program
- 23.19. Reading Standard Error from a Program
- 23.20. Locking a File
- 23.21. Reading and Writing Custom File Types
- 23.22. Reading and Writing Compressed Files
-
24. Directories
- 24.0. Introduction
- 24.1. Getting and Setting File Timestamps
- 24.2. Getting File Information
- 24.3. Changing File Permissions or Ownership
- 24.4. Splitting a Filename into Its Component Parts
- 24.5. Deleting a File
- 24.6. Copying or Moving a File
- 24.7. Processing All Files in a Directory
- 24.8. Getting a List of Filenames Matching a Pattern
- 24.9. Processing All Files in a Directory Recursively
- 24.10. Making New Directories
- 24.11. Removing a Directory and Its Contents
- 24.12. Program: Web Server Directory Listing
- 24.13. Program: Site Search
- 25. Command-Line PHP
- 26. PEAR and PECL
- Index
- About the Authors
- Colophon
- Copyright
Product information
- Title: PHP Cookbook, 2nd Edition
- Author(s):
- Release date: August 2006
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596101015
You might also like
book
PHP Cookbook, 3rd Edition
Want to understand a certain PHP programming technique? Or learn how to accomplish a particular task? …
book
PHP Cookbook
If you're a PHP developer looking for proven solutions to common problems, this cookbook provides code …
book
PHP Cookbook
PHP is a simple yet powerful open source scripting language that has become a big player …
book
Programming PHP, 4th Edition
Why is PHP the most widely used programming language on the web? This updated edition teaches …