PHP 5 Recipes: A Problem-Solution Approach

Book description

What you hold in your hands is the answer to all your PHP 5 needs. We have written PHP 5 Recipes with a number of purposes in mind.

First, this book is a source of instant solutions, including countless pieces of useful code that you can just copy and paste into your own applications, giving you answers fast and saving you hours of coding time.

Second, this book is a useful reference to the most important aspects of the PHP 5 language, including the vital functions you know and love from previous versions of PHP, as well as the functions introduced in PHP 5.

Finally, this book explains the PHP 5 functionality in detail, including the vastly improved object-oriented capabilities and the new MySQLi database extension.

We are confident PHP 5 Recipes will be a useful and welcome companion throughout your PHP journey, keeping you on the cutting edge of PHP development, ahead of the competition, and giving you all the answers you need, when you need them.

Table of contents

  1. Title Page
  2. Contents at a Glance
  3. Contents
  4. About the Authors
  5. About the Technical Reviewer
  6. Introduction
  7. CHAPTER 1: Overview of PHP Data Types and Concepts
    1. 1-1. Variables
    2. 1-2. Numbers
    3. 1-3. Arrays
    4. 1-4. Strings
    5. 1-5. Regular Expressions
    6. 1-6. Functions
    7. 1-7. Project: Finding the Data Type of a Value
    8. 1-8. Project: Discovering What Variables, Constants, Functions, Classes, and Interfaces Are Available
    9. 1-9. Getting Information About the Current Script
    10. Summary
    11. Looking Ahead
  8. CHAPTER 2: Overview of Classes, Objects, and Interfaces
    1. Understanding Basic Concepts
    2. 2-1. Creating Instances Using Constructors
    3. 2-2. Using Default Constructors
    4. 2-3. Setting Object Properties
    5. 2-4. Controlling Access to Class Members
    6. 2-5. Using Static Members and the self Keyword
    7. 2-6. Using Class Constants
    8. 2-7. Extending Classes
    9. 2-8. Using Abstract Classes and Methods
    10. 2-9. Using Interfaces
    11. 2-10. Using Class Destructors
    12. 2-11. Using Exceptions
    13. Getting Information About Classes and Objects
    14. Using Class and Object Functions
    15. 2-12. Checking for the Existence of Classes and Interfaces Using class_exists() and interface_exists()
    16. 2-13. Listing Methods and Interfaces Using get_class_methods()
    17. 2-14. Obtaining Variable Names
    18. 2-15. Determining Whether an Object Is an Instance of a Particular Class
    19. 2-16. Listing Currently Loaded Interfaces and Classes
    20. Using the Class Reflection API
    21. 2-17. Obtaining a Dump of the Reflection API
    22. 2-18. Performing Dynamic Class Instantiation
    23. 2-19. Using the Reflection API to Deconstruct the Shape Class
    24. Summary
    25. Looking Ahead
  9. CHAPTER 3: Performing Math Operations
    1. 3-1. Numeric Data Types
    2. 3-2. Random Numbers
    3. 3-3. Logarithms and Exponents
    4. 3-4. Trigonometric Functions
    5. 3-5. Formatting of Numeric Data
    6. 3-6. Math Libraries
    7. 3-7. A Static Math Class
    8. Summary
    9. Looking Ahead
  10. CHAPTER 4: Working with Arrays
    1. 4-1. Creating Arrays
    2. 4-2. Accessing Array Elements
    3. 4-3. Creating Multidimensional Arrays
    4. 4-4. Using Array Keys
    5. 4-5. Initializing an Array As a Range or Sequence of Values
    6. Outputting Arrays
    7. 4-6. Outputting an Array As a String
    8. 4-7. Outputting Using array_values() and array_keys() for Backward Compatibility
    9. 4-8. Outputting an Array As a Tree
    10. Adding New Elements to Arrays
    11. 4-9. Adding an Element to the End of an Array
    12. 4-10. Appending One Array to Another
    13. 4-11. Comparing Arrays
    14. 4-12. Adding an Element to the Beginning of an Array
    15. 4-13. Inserting New Values at an Arbitrary Point in an Indexed Array
    16. Getting and Setting the Size of an Array
    17. 4-14. Counting Array Elements
    18. 4-15. Setting an Array's Size
    19. Traversing Arrays
    20. 4-16. Looping Through an Associative Array Using foreach
    21. 4-17. Looping Through a Compact Indexed Array Using for and count()
    22. 4-18. Looping Through a Sparse Array
    23. Removing Elements from Arrays
    24. 4-19. Removing the First or Last Element from an Array
    25. 4-20. Removing One or More Arbitrary Array Elements
    26. 4-21. Extracting a Portion of an Array
    27. 4-22. Extracting Values from Arrays with extract()
    28. 4-23. Extracting Values from an Array Using list()
    29. 4-24. Combining Arrays
    30. 4-25. Obtaining Array Keys and Values
    31. 4-26. Working with Unique Values
    32. 4-27. Getting and Displaying Counts of Array Values
    33. Finding and Working with Array Values
    34. 4-28. Determining Whether an Element Is in an Array
    35. 4-29. Testing for the Existence of a Key in an Array
    36. 4-30. Obtaining Array Keys with a Given Value
    37. 4-31. Finding the Greatest and Least Values in an Array
    38. 4-32. Finding the Sum and Average of the Values in an Array
    39. Applying Functions to Arrays
    40. 4-33. Applying Functions to Array Elements Using array_walk()
    41. 4-34. Applying Functions to Array Elements Using array_map()
    42. 4-35. Filtering Arrays Using array_filter()
    43. Sorting Arrays
    44. 4-36. Sorting an Array by Its Values
    45. 4-37. Sorting an Array by Its Keys
    46. 4-38. Reversing an Array Using arsort()
    47. 4-39. Reversing an Array Using krsort()
    48. 4-40. Reversing an Array Using array_reverse()
    49. 4-41. Randomizing an Array Using shuffle(), kshuffle(), and array_rand()
    50. 4-42. Sorting an Array Using Comparison Functions
    51. 4-43. Sorting Multidimensional Arrays
    52. 4-44. Sorting Multiple Arrays
    53. Finding Permutations and Combinations
    54. 4-45. Finding All Permutations of an Array's Elements
    55. 4-46. Finding All Combinations of an Array's Elements
    56. Summary
    57. Looking Ahead
  11. CHAPTER 5: Working with Dates and Times
    1. Overview of PHP 5's Date and Time Functions
    2. Displaying Dates and Times
    3. 5-1. Displaying Human-Readable Dates and Times
    4. 5-2. Displaying Arbitrary Dates and Times
    5. 5-3. Converting Human-Readable Dates Into Unix Timestamps Using strtotime()
    6. 5-4. Finding the Date for a Weekday
    7. 5-5. Getting the Day and Week of the Year
    8. 5-6. Determining Whether a Given Year Is a Leap Year
    9. 5-7. Getting Times and Dates of Files
    10. 5-8. Setting Time Zones and GMT/UTC
    11. 5-9. Displaying Times and Dates in Other Languages
    12. 5-10. Generating Localized GMT/UTC Time and Date Strings
    13. 5-11. Obtaining the Difference Between Two Dates
    14. 5-12. Project: Constructing and Using a Date Class
    15. 5-13. Extending the Date Class
    16. Summary
    17. Looking Ahead
  12. CHAPTER 6: Working with Strings
    1. Manipulating Substrings
    2. 6-1. Testing for Substrings
    3. 6-2. Counting the Occurrences of a Substring
    4. 6-3. Accessing Substrings
    5. 6-4. Using Substring Alternatives
    6. 6-5. Replacing Substrings
    7. Processing Strings
    8. 6-6. Joining and Disassembling Strings
    9. 6-7. Reversing Strings
    10. 6-8. Controlling Case
    11. 6-9. Trimming Blank Spaces
    12. 6-10. Wrapping Text
    13. 6-11. Checking String Length
    14. 6-12. Comparing Strings
    15. 6-13. Comparing Sound
    16. Project: Creating and Using a String Class
    17. 6-14. Using a Page Reader Class
    18. Summary
    19. Looking Ahead
  13. CHAPTER 7: Working with Files and Directories
    1. Working with Files
    2. 7-1. Opening Files
    3. 7-2. Reading from Files
    4. 7-3. Writing to Files
    5. 7-4. Closing Files
    6. 7-5. Reading and Writing Comma-Separated Data
    7. 7-6. Reading Fixed-Width Delimited Data
    8. 7-7. Reading and Writing Binary Data in a File
    9. 7-8. Getting the Number of Lines in a File
    10. 7-9. Getting the Number of Characters, Words, or Paragraphs in a File
    11. 7-10. Project: Creating and Using a File Class
    12. Working with Directories
    13. 7-11. Listing All Files in the Current Directory
    14. 7-12. Listing All Files of a Certain Type
    15. 7-13. Sorting Files by Date
    16. 7-14. Generating a Recursive Directory Listing
    17. 7-15. Using the SPL DirectoryIterator Object
    18. Summary
    19. Looking Ahead
  14. CHAPTER 8: Working with Dynamic Imaging
    1. Working with Image Types
    2. 8-1. Working with JPGs
    3. 8-2. Working with GIFs
    4. 8-3. Working with PNGs
    5. Working with Image Libraries
    6. Creating an Image from Scratch
    7. 8-4. Creating a Blank Canvas
    8. 8-5. Creating and Using Colors
    9. 8-6. Creating and Applying Different Shapes and Patterns
    10. 8-7. Outputting an Image
    11. Creating an Image from an Existing Image
    12. 8-8. Loading an Existing Image
    13. 8-9. Applying Modifications to an Existing Image
    14. 8-10. Saving and Outputting the Modified Image
    15. Using TrueType Fonts
    16. 8-11. Loading Fonts
    17. 8-12. Applying TrueType Fonts to an Image
    18. 8-13. Project: Creating and Using a Dynamic Thumbnail Class
    19. Summary
    20. Looking Ahead
  15. CHAPTER 9: Using Regular Expressions
    1. Overview of Regular Expression Syntax
    2. Qualifiers
    3. Ranges
    4. Line Anchors
    5. An Escape
    6. Saying OR
    7. Character Classes
    8. POSIX vs. PCRE
    9. POSIX
    10. PCRE
    11. Putting Regular Expressions to Work
    12. 9-1. Using String Matching vs. Pattern Matching
    13. 9-2. Finding the nth Occurrence of a Match
    14. 9-3. Matching with Greedy vs. Nongreedy Expressions
    15. 9-4. Matching a Valid IP Address
    16. 9-5. Validating Pascal Case Names
    17. 9-6. Validating U.S. Currency
    18. 9-7. Formatting a Phone Number
    19. 9-8. Finding Repeated Words
    20. 9-9. Finding Words Not Followed by Other Words
    21. 9-10. Matching a Valid E-mail Address
    22. 9-11. Finding All Matching Lines in a File
    23. 9-12. Finding Lines with an Odd Number of Quotes
    24. 9-13. Capturing Text Inside HTML or XML Tags
    25. 9-14. Escaping Special Characters
    26. 9-15. Replacing URLs with Links
    27. 9-16. Replacing Smart Quotes with Straight Quotes
    28. 9-17. Testing the Complexity of Passwords
    29. 9-18. Matching GUIDs/UUIDs
    30. 9-19. Reading Records with a Delimiter
    31. 9-20. Creating Your Own RegExp Class
    32. Summary
    33. Looking Ahead
  16. CHAPTER 10: Working with Variables
    1. 10-1. Using Variable Types
    2. 10-2. Assigning and Comparing
    3. 10-3. Typecasting
    4. 10-4. Using Constants
    5. 10-5. Defining Variable Scope
    6. 10-6. Parsing Values to Functions
    7. 10-7. Using Dynamic Variable and Function Names
    8. 10-8. Encapsulating Complex Data Types
    9. 10-9. Sharing Variables Between Processes
    10. 10-10. Debugging
    11. Summary
    12. Looking Ahead
  17. CHAPTER 11: Using Functions
    1. 11-1. Accessing Function Parameters
    2. 11-2. Setting Default Values for Function Parameters
    3. 11-3. Passing Values by Reference
    4. 11-4. Creating Functions That Take a Variable Number of Arguments
    5. 11-5. Returning More Than One Value
    6. 11-6. Returning Values by Reference
    7. 11-7. Returning Failure
    8. 11-8. Calling Variable Functions
    9. 11-9. Accessing a Global Variable from Within a Function
    10. 11-10. Creating Dynamic Functions
    11. Summary
    12. Looking Ahead
  18. CHAPTER 12: Understanding Web Basics
    1. Using Cookies
    2. 12-1. Setting Cookies
    3. 12-2. Reading Cookies
    4. 12-3. Deleting Cookies
    5. 12-4. Writing and Using a Cookie Class
    6. Using HTTP Headers
    7. 12-5. Redirecting to a Different Location
    8. 12-6. Sending Content Types Other Than HTML
    9. 12-7. Forcing File “Save As” Downloads
    10. Using Sessions
    11. 12-8. Implementing Sessions
    12. 12-9. Storing Simple Data Types in Sessions
    13. 12-10. Storing Complex Data Types in Sessions
    14. 12-11. Detecting Browsers
    15. Using Querystrings
    16. 12-12. Using Querystrings
    17. 12-13. Passing Numeric Values in a Querystring
    18. 12-14. Passing String Values in a Querystring
    19. 12-15. Passing Complex Values in a Querystring
    20. Authenticating Your Users
    21. 12-16. Setting Up HTTP-Based Authentication
    22. 12-17. Setting Up Cookie Authentication
    23. Using Environment and Configuration Variables
    24. 12-18. Reading Environment and Configuration Variables
    25. 12-19. Setting Environment and Configuration Variables
    26. Summary
    27. Looking Ahead
  19. CHAPTER 13: Creating and Using Forms
    1. Understanding Common Form Issues
    2. 13-1. GET vs. POST
    3. 13-2. Superglobals vs. Globals
    4. 13-3. Validating Form Input
    5. 13-4. Working with Multipage Forms
    6. 13-5. Redisplaying Forms with Preserved Information and Error Messages
    7. Preventing Multiple Submissions of a Form
    8. 13-6. Preventing Multiple Submissions on the Server Side
    9. 13-7. Preventing Multiple Submissions on the Client Side
    10. 13-8. Performing File Uploads
    11. 13-9. Handling Special Characters
    12. 13-10. Creating Form Elements with Multiple Options
    13. 13-11. Creating Form Elements Based on the Current Time and/or Date
    14. Summary
    15. Looking Ahead
  20. CHAPTER 14: Working with Markup
    1. 14-1. Understanding Markup Concepts
    2. 14-2. Manually Generating Markup
    3. 14-3. Using DOM to Generate Markup
    4. 14-4. Creating and Setting Attributes
    5. 14-5. Parsing XML
    6. 14-6. Transforming XML with XSL
    7. 14-7. Using RSS Feeds
    8. 14-8. Using WDDX
    9. 14-9. Using SOAP
    10. Summary
    11. Looking Ahead
  21. CHAPTER 15: Using MySQL Databases in PHP 5
    1. Basic Database Concepts
    2. 15-1. Connecting to a MySQL Database
    3. 15-2. Querying the Database
    4. 15-3. Retrieving and Displaying Results
    5. 15-4. Modifying Data
    6. 15-5. Deleting Data
    7. 15-6. Building Queries on the Fly
    8. The mysqli Extension vs. the PHP 4 MySQL Extension
    9. 15-7. Using the mysqli Object-Oriented API
    10. 15-8. Using Exceptions to Handle Database Errors
    11. 15-9. Project: Displaying Linked Search Results
    12. 15-10. Displaying Results in a Form
    13. Project: Bridging the Gap Between mysql and mysqli
    14. 15-11. Discovering Which Extension Is Being Used
    15. 15-12. Writing a Wrapper Class to Bridge the Gap
    16. 15-13. Project: Going from MySQL to XML and from XML to MySQL
    17. Summary
    18. Looking Ahead
  22. CHAPTER 16: Communicating with Internet Services
    1. 16-1. Sending Internet Mail
    2. 16-2. Project: Sending an E-mail with a Mail Class
    3. 16-3. Reading Mail with IMAP or POP3
    4. 16-4. Getting and Putting Files with FTP
    5. 16-5. Performing DNS Lookups
    6. 16-6. Checking Whether a Host Is Alive
    7. 16-7. Getting Information About a Domain Name
    8. Summary
  23. Index

Product information

  • Title: PHP 5 Recipes: A Problem-Solution Approach
  • Author(s): Lee Babin, Nathan A. Good, Frank M. Kromann, Jon Stephens
  • Release date: October 2005
  • Publisher(s): Apress
  • ISBN: 9781590595091