PHP Cookbook

Book description

If you're a PHP developer looking for proven solutions to common problems, this cookbook provides code recipes to help you resolve a variety of coding issues. PHP is a remarkably easy language to work with, which explains why it powers more than 75% of the websites online today. It's also incredibly forgiving of programming mistakes, which can perpetuate reuse of questionable code.

By leveraging modern versions of PHP through version 8.2, author Eric Mann provides self-contained recipes that will enable you to solve the problems you face in your day-to-day work. You'll also find established patterns and examples that any developer can follow for addressing common problems with PHP. With these recipes, you'll quickly identify and resolve complicated issues without having to reinvent the wheel.

This practical guide will help you:

  • Build efficient applications composed of functions and objects
  • Explore the type system of modern PHP
  • Understand key concepts such as encryption, error handling, debugging, and performance tuning
  • Examine the PHP package/extension ecosystem
  • Learn how to build basic web and command-line applications
  • Work securely with files on a machine, both encrypted and in plain text

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who This Book Is For
    2. Navigating This Book
    3. Conventions Used in This Book
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. Variables
    1. 1.1. Defining Constants
    2. 1.2. Creating Variable Variables
    3. 1.3. Swapping Variables in Place
  3. 2. Operators
    1. 2.1. Using a Ternary Operator Instead of an If-Else Block
    2. 2.2. Coalescing Potentially Null Values
    3. 2.3. Comparing Identical Values
    4. 2.4. Using the Spaceship Operator to Sort Values
    5. 2.5. Suppressing Diagnostic Errors with an Operator
    6. 2.6. Comparing Bits Within Integers
  4. 3. Functions
    1. 3.1. Accessing Function Parameters
    2. 3.2. Setting a Function’s Default Parameters
    3. 3.3. Using Named Function Parameters
    4. 3.4. Enforcing Function Argument and Return Typing
    5. 3.5. Defining a Function with a Variable Number of Arguments
    6. 3.6. Returning More Than One Value
    7. 3.7. Accessing Global Variables from Within a Function
    8. 3.8. Managing State Within a Function Across Multiple Invocations
    9. 3.9. Defining Dynamic Functions
    10. 3.10. Passing Functions as Parameters to Other Functions
    11. 3.11. Using Concise Function Definitions (Arrow Functions)
    12. 3.12. Creating a Function with No Return Value
    13. 3.13. Creating a Function That Does Not Return
  5. 4. Strings
    1. 4.1. Accessing Substrings Within a Larger String
    2. 4.2. Extracting One String from Within Another
    3. 4.3. Replacing Part of a String
    4. 4.4. Processing a String One Byte at a Time
    5. 4.5. Generating Random Strings
    6. 4.6. Interpolating Variables Within a String
    7. 4.7. Concatenating Multiple Strings Together
    8. 4.8. Managing Binary Data Stored in Strings
  6. 5. Numbers
    1. 5.1. Validating a Number Within a Variable
    2. 5.2. Comparing Floating-Point Numbers
    3. 5.3. Rounding Floating-Point Numbers
    4. 5.4. Generating Truly Random Numbers
    5. 5.5. Generating Predictable Random Numbers
    6. 5.6. Generating Weighted Random Numbers
    7. 5.7. Calculating Logarithms
    8. 5.8. Calculating Exponents
    9. 5.9. Formatting Numbers as Strings
    10. 5.10. Handling Very Large or Very Small Numbers
    11. 5.11. Converting Numbers Between Numerical Bases
  7. 6. Dates and Times
    1. 6.1. Finding the Current Date and Time
    2. 6.2. Formatting Dates and Times
    3. 6.3. Converting Dates and Times to Unix Timestamps
    4. 6.4. Converting from Unix Timestamps to Date and Time Parts
    5. 6.5. Computing the Difference Between Two Dates
    6. 6.6. Parsing Dates and Times from Arbitrary Strings
    7. 6.7. Validating a Date
    8. 6.8. Adding to or Subtracting from a Date
    9. 6.9. Calculating Times Across Time Zones
  8. 7. Arrays
    1. 7.1. Associating Multiple Elements per Key in an Array
    2. 7.2. Initializing an Array with a Range of Numbers
    3. 7.3. Iterating Through Items in an Array
    4. 7.4. Deleting Elements from Associative and Numeric Arrays
    5. 7.5. Changing the Size of an Array
    6. 7.6. Appending One Array to Another
    7. 7.7. Creating an Array from a Fragment of an Existing Array
    8. 7.8. Converting Between Arrays and Strings
    9. 7.9. Reversing an Array
    10. 7.10. Sorting an Array
    11. 7.11. Sorting an Array Based on a Function
    12. 7.12. Randomizing the Elements in an Array
    13. 7.13. Applying a Function to Every Element of an Array
    14. 7.14. Reducing an Array to a Single Value
    15. 7.15. Iterating over Infinite or Very Large/Expensive Arrays
  9. 8. Classes and Objects
    1. 8.1. Instantiating Objects from Custom Classes
    2. 8.2. Constructing Objects to Define Defaults
    3. 8.3. Defining Read-Only Properties in a Class
    4. 8.4. Deconstructing Objects to Clean Up After the Object Is No Longer Needed
    5. 8.5. Using Magic Methods to Provide Dynamic Properties
    6. 8.6. Extending Classes to Define Additional Functionality
    7. 8.7. Forcing Classes to Exhibit Specific Behavior
    8. 8.8. Creating Abstract Base Classes
    9. 8.9. Preventing Changes to Classes and Methods
    10. 8.10. Cloning Objects
    11. 8.11. Defining Static Properties and Methods
    12. 8.12. Introspecting Private Properties or Methods Within an Object
    13. 8.13. Reusing Arbitrary Code Between Classes
  10. 9. Security and Encryption
    1. 9.1. Filtering, Validating, and Sanitizing User Input
    2. 9.2. Keeping Sensitive Credentials Out of Application Code
    3. 9.3. Hashing and Validating Passwords
    4. 9.4. Encrypting and Decrypting Data
    5. 9.5. Storing Encrypted Data in a File
    6. 9.6. Cryptographically Signing a Message to Be Sent to Another Application
    7. 9.7. Verifying a Cryptographic Signature
  11. 10. File Handling
    1. 10.1. Creating or Opening a Local File
    2. 10.2. Reading a File into a String
    3. 10.3. Reading a Specific Slice of a File
    4. 10.4. Modifying a File in Place
    5. 10.5. Writing to Many Files Simultaneously
    6. 10.6. Locking a File to Prevent Access or Modification by Another Process
  12. 11. Streams
    1. 11.1. Streaming Data to/from a Temporary File
    2. 11.2. Reading from the PHP Input Stream
    3. 11.3. Writing to the PHP Output Stream
    4. 11.4. Reading from One Stream and Writing to Another
    5. 11.5. Composing Different Stream Handlers Together
    6. 11.6. Writing a Custom Stream Wrapper
  13. 12. Error Handling
    1. 12.1. Finding and Fixing Parse Errors
    2. 12.2. Creating and Handling Custom Exceptions
    3. 12.3. Hiding Error Messages from End Users
    4. 12.4. Using a Custom Error Handler
    5. 12.5. Logging Errors to an External Stream
  14. 13. Debugging and Testing
    1. 13.1. Using a Debugger Extension
    2. 13.2. Writing a Unit Test
    3. 13.3. Automating Unit Tests
    4. 13.4. Using Static Code Analysis
    5. 13.5. Logging Debugging Information
    6. 13.6. Dumping Variable Contents as Strings
    7. 13.7. Using the Built-in Web Server to Quickly Run an Application
    8. 13.8. Using Unit Tests to Detect Regressions in a Version-Controlled Project with git-bisect
  15. 14. Performance Tuning
    1. 14.1. Timing Function Execution
    2. 14.2. Benchmarking the Performance of an Application
    3. 14.3. Accelerating an Application with an Opcode Cache
  16. 15. Packages and Extensions
    1. 15.1. Defining a Composer Project
    2. 15.2. Finding Composer Packages
    3. 15.3. Installing and Updating Composer Packages
    4. 15.4. Installing Native PHP Extensions
  17. 16. Databases
    1. 16.1. Relational Databases
    2. 16.2. Key-Value Stores
    3. 16.3. Graph Databases
    4. 16.4. Document Databases
    5. 16.5. Connecting to an SQLite Database
    6. 16.6. Using PDO to Connect to an External Database Provider
    7. 16.7. Sanitizing User Input for a Database Query
    8. 16.8. Mocking Data for Integration Testing with a Database
    9. 16.9. Querying an SQL Database with the Eloquent ORM
  18. 17. Asynchronous PHP
    1. 17.1. Fetching Data from Remote APIs Asynchronously
    2. 17.2. Waiting on the Results of Multiple Asynchronous Operations
    3. 17.3. Interrupting One Operation to Run Another
    4. 17.4. Running Code in a Separate Thread
    5. 17.5. Sending and Receiving Messages Between Separate Threads
    6. 17.6. Using a Fiber to Manage the Contents from a Stream
  19. 18. PHP Command Line
    1. 18.1. Parsing Program Arguments
    2. 18.2. Reading Interactive User Input
    3. 18.3. Colorizing Console Output
    4. 18.4. Creating a Command-Line Application with Symfony Console
    5. 18.5. Using PHP’s Native Read-Eval-Print-Loop
  20. Index
  21. About the Author

Product information

  • Title: PHP Cookbook
  • Author(s): Eric A. Mann
  • Release date: May 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098121327