PHP Cookbook: Modern Code Solutions for Professional Developers

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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

Author(s): Eric Mann
Edition: 1
Publisher: O'Reilly Media
Year: 2023

Language: English
Commentary: Publisher PDF | Revision History for the First Edition: 2023-05-16: First Release
Pages: 431
City: Sebastopol, CA
Tags: PHP; PHP Programming Language; Web Programming; Web Development; Server Side Development

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