Book description
Want to understand a certain PHP programming technique? Or learn how to accomplish a particular task? This cookbook is the first place to look. With more than 350 code-rich recipes revised for PHP 5.4 and 5.5, this third edition provides updated solutions for generating dynamic web content—everything from using basic data types to querying databases, and from calling RESTful APIs to testing and securing your site.
Table of contents
- Preface
-
1. Strings
- Introduction
- Accessing Substrings
- Extracting Substrings
- Replacing Substrings
- Processing a String One Byte at a Time
- Reversing a String by Word or Byte
- Generating a Random String
- Expanding and Compressing Tabs
- Controlling Case
- Interpolating Functions and Expressions Within Strings
- Trimming Blanks from a String
- Generating Comma-Separated Data
- Parsing Comma-Separated Data
- Generating Fixed-Width Field Data Records
- Parsing Fixed-Width Field Data Records
- Taking Strings Apart
- Wrapping Text at a Certain Line Length
- Storing Binary Data in Strings
- Program: Downloadable CSV File
-
2. Numbers
- Introduction
- Checking Whether a Variable Contains a Valid Number
- Comparing Floating-Point Numbers
- Rounding Floating-Point Numbers
- Operating on a Series of Integers
- Generating Random Numbers Within a Range
- Generating Predictable Random Numbers
- Generating Biased Random Numbers
- Taking Logarithms
- Calculating Exponents
- Formatting Numbers
- Formatting Monetary Values
- Printing Correct Plurals
- Calculating Trigonometric Functions
- Doing Trigonometry in Degrees, Not Radians
- Handling Very Large or Very Small Numbers
- Converting Between Bases
- Calculating Using Numbers in Bases Other Than Decimal
- Finding the Distance Between Two Places
-
3. Dates and Times
- Introduction
- Finding the Current Date and Time
- Converting Time and Date Parts to an Epoch Timestamp
- Converting an Epoch Timestamp to Time and Date Parts
- Printing a Date or Time in a Specified Format
- Finding the Difference of Two Dates
- Finding the Day in a Week, Month, or Year
- Validating a Date
- Parsing Dates and Times from Strings
- Adding to or Subtracting from a Date
- Calculating Time with Time Zones and Daylight Saving Time
- Generating a High-Precision Time
- Generating Time Ranges
- Using Non-Gregorian Calendars
- Program: Calendar
-
4. Arrays
- Introduction
- Specifying an Array Not Beginning at Element 0
- Storing Multiple Elements per Key in an Array
- Initializing an Array to a Range of Integers
- Iterating Through an Array
- Deleting Elements from an Array
- Changing Array Size
- Appending One Array to Another
- Turning an Array into a String
- Printing an Array with Commas
- Checking if a Key Is in an Array
- Checking if an Element Is in an Array
- Finding the Position of a Value in an Array
- Finding Elements That Pass a Certain Test
- Finding the Largest or Smallest Valued Element in an Array
- Reversing an Array
- Sorting an Array
- Sorting an Array by a Computable Field
- Sorting Multiple Arrays
- Sorting an Array Using a Method Instead of a Function
- Randomizing an Array
- Removing Duplicate Elements from an Array
- Applying a Function to Each Element in an Array
- Finding the Union, Intersection, or Difference of Two Arrays
- Iterating Efficiently over Large or Expensive Datasets
- Accessing an Object Using Array Syntax
-
5. Variables
- Introduction
- Avoiding == Versus = Confusion
- Establishing a Default Value
- Exchanging Values Without Using Temporary Variables
- Creating a Dynamic Variable Name
- Persisting a Local Variable’s Value Across Function Invocations
- Sharing Variables Between Processes
- Encapsulating Complex Data Types in a String
- Dumping Variable Contents as Strings
-
6. Functions
- Introduction
- Accessing Function Parameters
- Setting Default Values for Function Parameters
- Passing Values by Reference
- Using Named Parameters
- Enforcing Types of Function Arguments
- Creating Functions That Take a Variable Number of Arguments
- Returning Values by Reference
- Returning More Than One Value
- Skipping Selected Return Values
- Returning Failure
- Calling Variable Functions
- Accessing a Global Variable Inside a Function
- Creating Dynamic Functions
-
7. Classes and Objects
- Introduction
- Instantiating Objects
- Defining Object Constructors
- Defining Object Destructors
- Implementing Access Control
- Preventing Changes to Classes and Methods
- Defining Object Stringification
- Requiring Multiple Classes to Behave Similarly
- Creating Abstract Base Classes
- Assigning Object References
- Cloning Objects
- Overriding Property Accesses
- Calling Methods on an Object Returned by Another Method
- Aggregating Objects
- Accessing Overridden Methods
- Creating Methods Dynamically
- Using Method Polymorphism
- Defining Class Constants
- Defining Static Properties and Methods
- Controlling Object Serialization
- Introspecting Objects
- Checking If an Object Is an Instance of a Specific Class
- Autoloading Class Files upon Object Instantiation
- Instantiating an Object Dynamically
- Program: whereis
-
8. Web Fundamentals
- Introduction
- Setting Cookies
- Reading Cookie Values
- Deleting Cookies
- Building a Query String
- Reading the POST Request Body
- Using HTTP Basic or Digest Authentication
- Using Cookie Authentication
- Reading an HTTP Header
- Writing an HTTP Header
- Sending a Specific HTTP Status Code
- Redirecting to a Different Location
- Flushing Output to the Browser
- Buffering Output to the Browser
- Compressing Web Output
- Reading Environment Variables
- Setting Environment Variables
- Communicating Within Apache
- Redirecting Mobile Browsers to a Mobile Optimized Site
- Program: Website Account (De)activator
- Program: Tiny Wiki
- Program: HTTP Range
-
9. Forms
- Introduction
- Processing Form Input
- Validating Form Input: Required Fields
- Validating Form Input: Numbers
- Validating Form Input: Email Addresses
- Validating Form Input: Drop-Down Menus
- Validating Form Input: Radio Buttons
- Validating Form Input: Checkboxes
- Validating Form Input: Dates and Times
- Validating Form Input: Credit Cards
- Preventing Cross-Site Scripting
- Processing Uploaded Files
- Working with Multipage Forms
- Redisplaying Forms with Inline Error Messages
- Guarding Against Multiple Submissions of the Same Form
- Preventing Global Variable Injection
- Handling Remote Variables with Periods in Their Names
- Using Form Elements with Multiple Options
- Creating Drop-Down Menus Based on the Current Date
-
10. Database Access
- Introduction
- Using DBM Databases
- Using an SQLite Database
- Connecting to an SQL Database
- Querying an SQL Database
- Retrieving Rows Without a Loop
- Modifying Data in an SQL Database
- Repeating Queries Efficiently
- Finding the Number of Rows Returned by a Query
- Escaping Quotes
- Logging Debugging Information and Errors
- Creating Unique Identifiers
- Building Queries Programmatically
- Making Paginated Links for a Series of Records
- Caching Queries and Results
- Accessing a Database Connection Anywhere in Your Program
- Program: Storing a Threaded Message Board
- Using Redis
- 11. Sessions and Data Persistence
-
12. XML
- Introduction
- Generating XML as a String
- Generating XML with DOM
- Parsing Basic XML Documents
- Parsing Complex XML Documents
- Parsing Large XML Documents
- Extracting Information Using XPath
- Transforming XML with XSLT
- Setting XSLT Parameters from PHP
- Calling PHP Functions from XSLT Stylesheets
- Validating XML Documents
- Handling Content Encoding
- Reading RSS and Atom Feeds
- Writing RSS Feeds
- Writing Atom Feeds
-
13. Web Automation
- Introduction
- Marking Up a Web Page
- Cleaning Up Broken or Nonstandard HTML
- Extracting Links from an HTML File
- Converting Plain Text to HTML
- Converting HTML to Plain Text
- Removing HTML and PHP Tags
- Responding to an Ajax Request
- Integrating with JavaScript
- Program: Finding Stale Links
- Program: Finding Fresh Links
-
14. Consuming RESTful APIs
- Introduction
- Fetching a URL with the GET Method
- Fetching a URL with the POST Method and Form Data
- Fetching a URL with an Arbitrary Method and POST Body
- Fetching a URL with Cookies
- Fetching a URL with Arbitrary Headers
- Fetching a URL with a Timeout
- Fetching an HTTPS URL
- Debugging the Raw HTTP Exchange
- Making an OAuth 1.0 Request
- Making an OAuth 2.0 Request
- 15. Serving RESTful APIs
- 16. Internet Services
-
17. Graphics
- Introduction
- Drawing Lines, Rectangles, and Polygons
- Drawing Arcs, Ellipses, and Circles
- Drawing with Patterned Lines
- Drawing Text
- Drawing Centered Text
- Building Dynamic Images
- Getting and Setting a Transparent Color
- Overlaying Watermarks
- Creating Thumbnail Images
- Reading EXIF Data
- Serving Images Securely
- Program: Generating Bar Charts from Poll Results
-
18. Security and Encryption
- Introduction
- Preventing Session Fixation
- Protecting Against Form Spoofing
- Ensuring Input Is Filtered
- Avoiding Cross-Site Scripting
- Eliminating SQL Injection
- Keeping Passwords Out of Your Site Files
- Storing Passwords
- Dealing with Lost Passwords
- Verifying Data with Hashes
- Encrypting and Decrypting Data
- Storing Encrypted Data in a File or Database
- Sharing Encrypted Data with Another Website
- Detecting SSL
- Encrypting Email with GPG
-
19. Internationalization and Localization
- Introduction
- Determining the User’s Locale
- Localizing Text Messages
- Localizing Dates and Times
- Localizing Numbers
- Localizing Currency Values
- Localizing Images
- Localizing Included Files
- Sorting in a Locale-Aware Order
- Managing Localization Resources
- Setting the Character Encoding of Outgoing Data
- Setting the Character Encoding of Incoming Data
- Manipulating UTF-8 Text
-
20. Error Handling
- Introduction
- Finding and Fixing Parse Errors
- Creating Your Own Exception Classes
- Printing a Stack Trace
- Reading Configuration Variables
- Setting Configuration Variables
- Hiding Error Messages from Users
- Tuning Error Handling
- Using a Custom Error Handler
- Logging Errors
- Eliminating “headers already sent” Errors
- Logging Debugging Information
- 21. Software Engineering
- 22. Performance Tuning
-
23. Regular Expressions
- Introduction
- Switching from ereg to preg
- Matching Words
- Finding the nth Occurrence of a Match
- Choosing Greedy or Nongreedy Matches
- Finding All Lines in a File That Match a Pattern
- Capturing Text Inside HTML Tags
- Preventing Parentheses from Capturing Text
- Escaping Special Characters in a Regular Expression
- Reading Records with a Pattern Separator
- Using a PHP Function in a Regular Expression
-
24. Files
- Introduction
- Creating or Opening a Local File
- Creating a Temporary File
- Opening a Remote File
- Reading from Standard Input
- Reading a File into a String
- Counting Lines, Paragraphs, or Records in a File
- Processing Every Word in a File
- Picking a Random Line from a File
- Randomizing All Lines in a File
- Processing Variable-Length Text Fields
- Reading Configuration Files
- Modifying a File in Place Without a Temporary File
- Flushing Output to a File
- Writing to Standard Output
- Writing to Many Filehandles Simultaneously
- Escaping Shell Metacharacters
- Passing Input to a Program
- Reading Standard Output from a Program
- Reading Standard Error from a Program
- Locking a File
- Reading and Writing Custom File Types
- Reading and Writing Compressed Files
-
25. Directories
- Introduction
- Getting and Setting File Timestamps
- Getting File Information
- Changing File Permissions or Ownership
- Splitting a Filename into Its Component Parts
- Deleting a File
- Copying or Moving a File
- Processing All Files in a Directory
- Getting a List of Filenames Matching a Pattern
- Processing All Files in a Directory Recursively
- Making New Directories
- Removing a Directory and Its Contents
- Program: Web Server Directory Listing
- Program: Site Search
- 26. Command-Line PHP
- 27. Packages
- Index
- Colophon
- Copyright
Product information
- Title: PHP Cookbook, 3rd Edition
- Author(s):
- Release date: July 2014
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781449363758
You might also like
book
PHP Cookbook, 2nd Edition
When it comes to creating dynamic web sites, the open source PHP language is red-hot property: …
book
MySQL Cookbook, 4th Edition
For MySQL, the price of popularity comes with a flood of questions from users on how …
book
Programming PHP, 4th Edition
Why is PHP the most widely used programming language on the web? This updated edition teaches …
video
Modern JavaScript from the Beginning - Second Edition
This course is a comprehensive introduction to JavaScript, covering everything from the basics of variables, data …