Table of contents : Preface Who This Book Is For Navigating This Book Conventions Used in This Book O’Reilly Online Learning How to Contact Us Acknowledgments 1. Variables 1.1. Defining Constants 1.2. Creating Variable Variables 1.3. Swapping Variables in Place 2. Operators 2.1. Using a Ternary Operator Instead of an If-Else Block 2.2. Coalescing Potentially Null Values 2.3. Comparing Identical Values 2.4. Using the Spaceship Operator to Sort Values 2.5. Suppressing Diagnostic Errors with an Operator 2.6. Comparing Bits Within Integers 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 10.1. Creating or Opening a Local File 10.2. Reading a File into a String 10.3. Reading a Specific Slice of a File 10.4. Modifying a File in Place 10.5. Writing to Many Files Simultaneously 10.6. Locking a File to Prevent Access or Modification by Another Process 11. Streams 11.1. Streaming Data to/from a Temporary File 11.2. Reading from the PHP Input Stream 11.3. Writing to the PHP Output Stream 11.4. Reading from One Stream and Writing to Another 11.5. Composing Different Stream Handlers Together 11.6. Writing a Custom Stream Wrapper 12. Error Handling 12.1. Finding and Fixing Parse Errors 12.2. Creating and Handling Custom Exceptions 12.3. Hiding Error Messages from End Users 12.4. Using a Custom Error Handler 12.5. Logging Errors to an External Stream 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 14.1. Timing Function Execution 14.2. Benchmarking the Performance of an Application 14.3. Accelerating an Application with an Opcode Cache 15. Packages and Extensions 15.1. Defining a Composer Project 15.2. Finding Composer Packages 15.3. Installing and Updating Composer Packages 15.4. Installing Native PHP 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 18.1. Parsing Program Arguments 18.2. Reading Interactive User Input 18.3. Colorizing Console Output 18.4. Creating a Command-Line Application with Symfony Console 18.5. Using PHP’s Native Read-Eval-Print-Loop Index About the Author