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
Table of contents
- Preface
- 1. Variables
- 2. Operators
-
3. Functions
- 3.1. Accessing Function Parameters
- 3.2. Setting a Function’s Default Parameters
- 3.3. Using Named Function Parameters
- 3.4. Enforcing Function Argument and Return Typing
- 3.5. Defining a Function with a Variable Number of Arguments
- 3.6. Returning More Than One Value
- 3.7. Accessing Global Variables from Within a Function
- 3.8. Managing State Within a Function Across Multiple Invocations
- 3.9. Defining Dynamic Functions
- 3.10. Passing Functions as Parameters to Other Functions
- 3.11. Using Concise Function Definitions (Arrow Functions)
- 3.12. Creating a Function with No Return Value
- 3.13. Creating a Function That Does Not Return
-
4. Strings
- 4.1. Accessing Substrings Within a Larger String
- 4.2. Extracting One String from Within Another
- 4.3. Replacing Part of a String
- 4.4. Processing a String One Byte at a Time
- 4.5. Generating Random Strings
- 4.6. Interpolating Variables Within a String
- 4.7. Concatenating Multiple Strings Together
- 4.8. Managing Binary Data Stored in Strings
-
5. Numbers
- 5.1. Validating a Number Within a Variable
- 5.2. Comparing Floating-Point Numbers
- 5.3. Rounding Floating-Point Numbers
- 5.4. Generating Truly Random Numbers
- 5.5. Generating Predictable Random Numbers
- 5.6. Generating Weighted Random Numbers
- 5.7. Calculating Logarithms
- 5.8. Calculating Exponents
- 5.9. Formatting Numbers as Strings
- 5.10. Handling Very Large or Very Small Numbers
- 5.11. Converting Numbers Between Numerical Bases
-
6. Dates and Times
- 6.1. Finding the Current Date and Time
- 6.2. Formatting Dates and Times
- 6.3. Converting Dates and Times to Unix Timestamps
- 6.4. Converting from Unix Timestamps to Date and Time Parts
- 6.5. Computing the Difference Between Two Dates
- 6.6. Parsing Dates and Times from Arbitrary Strings
- 6.7. Validating a Date
- 6.8. Adding to or Subtracting from a Date
- 6.9. Calculating Times Across Time Zones
-
7. Arrays
- 7.1. Associating Multiple Elements per Key in an Array
- 7.2. Initializing an Array with a Range of Numbers
- 7.3. Iterating Through Items in an Array
- 7.4. Deleting Elements from Associative and Numeric Arrays
- 7.5. Changing the Size of an Array
- 7.6. Appending One Array to Another
- 7.7. Creating an Array from a Fragment of an Existing Array
- 7.8. Converting Between Arrays and Strings
- 7.9. Reversing an Array
- 7.10. Sorting an Array
- 7.11. Sorting an Array Based on a Function
- 7.12. Randomizing the Elements in an Array
- 7.13. Applying a Function to Every Element of an Array
- 7.14. Reducing an Array to a Single Value
- 7.15. Iterating over Infinite or Very Large/Expensive Arrays
-
8. Classes and Objects
- 8.1. Instantiating Objects from Custom Classes
- 8.2. Constructing Objects to Define Defaults
- 8.3. Defining Read-Only Properties in a Class
- 8.4. Deconstructing Objects to Clean Up After the Object Is No Longer Needed
- 8.5. Using Magic Methods to Provide Dynamic Properties
- 8.6. Extending Classes to Define Additional Functionality
- 8.7. Forcing Classes to Exhibit Specific Behavior
- 8.8. Creating Abstract Base Classes
- 8.9. Preventing Changes to Classes and Methods
- 8.10. Cloning Objects
- 8.11. Defining Static Properties and Methods
- 8.12. Introspecting Private Properties or Methods Within an Object
- 8.13. Reusing Arbitrary Code Between Classes
-
9. Security and Encryption
- 9.1. Filtering, Validating, and Sanitizing User Input
- 9.2. Keeping Sensitive Credentials Out of Application Code
- 9.3. Hashing and Validating Passwords
- 9.4. Encrypting and Decrypting Data
- 9.5. Storing Encrypted Data in a File
- 9.6. Cryptographically Signing a Message to Be Sent to Another Application
- 9.7. Verifying a Cryptographic Signature
- 10. File Handling
- 11. Streams
- 12. Error Handling
-
13. Debugging and Testing
- 13.1. Using a Debugger Extension
- 13.2. Writing a Unit Test
- 13.3. Automating Unit Tests
- 13.4. Using Static Code Analysis
- 13.5. Logging Debugging Information
- 13.6. Dumping Variable Contents as Strings
- 13.7. Using the Built-in Web Server to Quickly Run an Application
- 13.8. Using Unit Tests to Detect Regressions in a Version-Controlled Project with git-bisect
- 14. Performance Tuning
- 15. Packages and Extensions
-
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
- 16.6. Using PDO to Connect to an External Database Provider
- 16.7. Sanitizing User Input for a Database Query
- 16.8. Mocking Data for Integration Testing with a Database
- 16.9. Querying an SQL Database with the Eloquent ORM
-
17. Asynchronous PHP
- 17.1. Fetching Data from Remote APIs Asynchronously
- 17.2. Waiting on the Results of Multiple Asynchronous Operations
- 17.3. Interrupting One Operation to Run Another
- 17.4. Running Code in a Separate Thread
- 17.5. Sending and Receiving Messages Between Separate Threads
- 17.6. Using a Fiber to Manage the Contents from a Stream
- 18. PHP Command Line
- Index
- About the Author
Product information
- Title: PHP Cookbook
- Author(s):
- Release date: May 2023
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781098121327
You might also like
book
Programming PHP, 4th Edition
Why is PHP the most widely used programming language on the web? This updated edition teaches …
book
JavaScript Cookbook, 3rd Edition
Why reinvent the wheel every time you run into a problem with JavaScript? This cookbook is …
book
PHP 8 Basics: For Programming and Web Development
Take advantage of PHP 8's powerful features to create basic web applications, solve code tests (required …
book
Laravel: Up & Running, 2nd Edition
What sets Laravel apart from other PHP web frameworks? Speed and simplicity, for starters. This rapid …