If you're a PHP developer looking for proven solutions to common problems, this cookbook provides code recipes to help you resolve numerous scenarios. By leveraging modern versions of PHP through version 8.1, these self-contained recipes provide fully realized solutions that can help you solve similar problems in your day-to-day work.

Whether you're new to development or merely new to PHP, these recipes will help you unpack the most powerful features of this programming language. Author Eric Mann, a regular contributor to php[architec magazine, frequently makes presentations on software architecture and has built scalable projects for startups and Fortune 500 companies alike.

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

Table of Contents

  1. 1. Variables
    1. 1.1. Defining Constants
    2. 1.2. Creating Variable Variables
    3. 1.3. Swapping Variables In-place
  2. 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
  3. 3. Functions
    1. 3.1. Accessing function parameters
    2. 3.2. Settings 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
  4. 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
  5. About the Author