Book description
PHP is a simple yet powerful open source scripting language that has become a big player in web development. Over a million web sites, from large corporate sites to small personal sites, are using PHP to serve dynamic web content. PHP's broad feature set, approachable syntax, and support for different operating systems and web servers make it an ideal language for rapid web development. The PHP Cookbook is a collection of problems, solutions, and practical examples for PHP programmers. The book contains a unique and extensive collection of best practices for everyday PHP programming dilemmas. For every problem addressed in the book, there's a worked-out solution or "recipe" -- short, focused pieces of code that you can insert directly into your applications. But this book offers more than cut-and-paste code. You also get explanations of how and why the code works, so you can learn to adapt the problem-solving techniques to similar situations. The recipes in the PHP Cookbook range from simple tasks, such as sending a database query and fetching URLs, to entire programs that demonstrate complex tasks, such as printing HTML tables and generating bar charts. This book contains over 250 recipes on the following topics:
Working with basic data types, including strings, numbers, dates and times, and arrays
PHP building blocks, such as variables, functions, classes, and objects
Web programming, including forms, database access, and XML
Useful features like regular expressions, encryption and security, graphics, internationalization and localization, and Internet services
Working with files and directories
Command-line PHP and PHP-GTK
PEAR, the PHP Extension and Application Repository
This book contains an impressive collection of useful code for PHP programmers, from novices to advanced practitioners. Instead of poking around mailing lists, online documentation, and other sources, you can rely on the PHP Cookbook to provide quick solutions to common problems, so you can spend your time on those out-of-the-ordinary problems specific to your application.
Table of contents
-
PHP Cookbook
- Preface
-
1. Strings
- Introduction
- 1.1. Accessing Substrings
- 1.2. Replacing Substrings
- 1.3. Processing a String One Character at a Time
- 1.4. Reversing a String by Word or Character
- 1.5. Expanding and Compressing Tabs
- 1.6. Controlling Case
- 1.7. Interpolating Functions and Expressions Within Strings
- 1.8. Trimming Blanks from a String
- 1.9. Parsing Comma-Separated Data
- 1.10. Parsing Fixed-Width Delimited Data
- 1.11. Taking Strings Apart
- 1.12. Wrapping Text at a Certain Line Length
- 1.13. Storing Binary Data in Strings
-
2. Numbers
- Introduction
- 2.1. Checking Whether a String Contains a Valid Number
- 2.2. Comparing Floating-Point Numbers
- 2.3. Rounding Floating-Point Numbers
- 2.4. Operating on a Series of Integers
- 2.5. Generating Random Numbers Within a Range
- 2.6. Generating Biased Random Numbers
- 2.7. Taking Logarithms
- 2.8. Calculating Exponents
- 2.9. Formatting Numbers
- 2.10. Printing Correct Plurals
- 2.11. Calculating Trigonometric Functions
- 2.12. Doing Trigonometry in Degrees, not Radians
- 2.13. Handling Very Large or Very Small Numbers
- 2.14. Converting Between Bases
- 2.15. Calculating Using Numbers in Bases Other Than Decimal
-
3. Dates and Times
- Introduction
- 3.1. Finding the Current Date and Time
- 3.2. Converting Time and Date Parts to an Epoch Timestamp
- 3.3. Converting an Epoch Timestamp to Time and Date Parts
- 3.4. Printing a Date or Time in a Specified Format
- 3.5. Finding the Difference of Two Dates
- 3.6. Finding the Difference of Two Dates with Julian Days
- 3.7. Finding the Day in a Week, Month, Year, or the Week Number in a Year
- 3.8. Validating a Date
- 3.9. Parsing Dates and Times from Strings
- 3.10. Adding to or Subtracting from a Date
- 3.11. Calculating Time with Time Zones
- 3.12. Accounting for Daylight Saving Time
- 3.13. Generating a High-Precision Time
- 3.14. Generating Time Ranges
- 3.15. Using Non-Gregorian Calendars
- 3.16. Program: Calendar
-
4. Arrays
- Introduction
- 4.1. Specifying an Array Not Beginning at Element 0
- 4.2. Storing Multiple Elements per Key in an Array
- 4.3. Initializing an Array to a Range of Integers
- 4.4. Iterating Through an Array
- 4.5. Deleting Elements from an Array
- 4.6. Changing Array Size
- 4.7. Appending One Array to Another
- 4.8. Turning an Array into a String
- 4.9. Printing an Array with Commas
- 4.10. Checking if a Key Is in an Array
- 4.11. Checking if an Element Is in an Array
- 4.12. Finding the Position of an Element in an Array
- 4.13. Finding Elements That Pass a Certain Test
- 4.14. Finding the Largest or Smallest Valued Element in an Array
- 4.15. Reversing an Array
- 4.16. Sorting an Array
- 4.17. Sorting an Array by a Computable Field
- 4.18. Sorting Multiple Arrays
- 4.19. Sorting an Array Using a Method Instead of a Function
- 4.20. Randomizing an Array
- 4.21. Shuffling a Deck of Cards
- 4.22. Removing Duplicate Elements from an Array
- 4.23. Finding the Union, Intersection, or Difference of Two Arrays
- 4.24. Finding All Element Combinations of an Array
- 4.25. Finding All Permutations of an Array
- 4.26. Program: Printing an Array in a Horizontally Columned HTML Table
-
5. Variables
- Introduction
- 5.1. Avoiding == Versus = Confusion
- 5.2. Establishing a Default Value
- 5.3. Exchanging Values Without Using Temporary Variables
- 5.4. Creating a Dynamic Variable Name
- 5.5. Using Static Variables
- 5.6. Sharing Variables Between Processes
- 5.7. Encapsulating Complex Data Types as a String
- 5.8. Dumping Variable Contents as Strings
-
6. Functions
- Introduction
- 6.1. Accessing Function Parameters
- 6.2. Setting Default Values for Function Parameters
- 6.3. Passing Values by Reference
- 6.4. Using Named Parameters
- 6.5. Creating Functions That Take a Variable Number of Arguments
- 6.6. Returning Values by Reference
- 6.7. Returning More Than One Value
- 6.8. Skipping Selected Return Values
- 6.9. Returning Failure
- 6.10. Calling Variable Functions
- 6.11. Accessing a Global Variable Inside a Function
- 6.12. Creating Dynamic Functions
-
7. Classes and Objects
- Introduction
- 7.1. Instantiating Objects
- 7.2. Defining Object Constructors
- 7.3. Destroying an Object
- 7.4. Cloning Objects
- 7.5. Assigning Object References
- 7.6. Calling Methods on an Object Returned by Another Method
- 7.7. Accessing Overridden Methods
- 7.8. Using Property Overloading
- 7.9. Using Method Polymorphism
- 7.10. Finding the Methods and Properties of an Object
- 7.11. Adding Properties to a Base Object
- 7.12. Creating a Class Dynamically
- 7.13. Instantiating an Object Dynamically
-
8. Web Basics
- Introduction
- 8.1. Setting Cookies
- 8.2. Reading Cookie Values
- 8.3. Deleting Cookies
- 8.4. Redirecting to a Different Location
- 8.5. Using Session Tracking
- 8.6. Storing Sessions in a Database
- 8.7. Detecting Different Browsers
- 8.8. Building a GET Query String
- 8.9. Using HTTP Basic Authentication
- 8.10. Using Cookie Authentication
- 8.11. Flushing Output to the Browser
- 8.12. Buffering Output to the Browser
- 8.13. Compressing Web Output with gzip
- 8.14. Hiding Error Messages from Users
- 8.15. Tuning Error Handling
- 8.16. Using a Custom Error Handler
- 8.17. Logging Errors
- 8.18. Eliminating “headers already sent” Errors
- 8.19. Logging Debugging Information
- 8.20. Reading Environment Variables
- 8.21. Setting Environment Variables
- 8.22. Reading Configuration Variables
- 8.23. Setting Configuration Variables
- 8.24. Communicating Within Apache
- 8.25. Profiling Code
- 8.26. Program: Website Account (De)activator
- 8.27. Program: Abusive User Checker
-
9. Forms
- Introduction
- 9.1. Processing Form Input
- 9.2. Validating Form Input
- 9.3. Working with Multipage Forms
- 9.4. Redisplaying Forms with Preserved Information and Error Messages
- 9.5. Guarding Against Multiple Submission of the Same Form
- 9.6. Processing Uploaded Files
- 9.7. Securing PHP’s Form Processing
- 9.8. Escaping Control Characters from User Data
- 9.9. Handling Remote Variables with Periods in Their Names
- 9.10. Using Form Elements with Multiple Options
- 9.11. Creating Dropdown Menus Based on the Current Date
-
10. Database Access
- Introduction
- 10.1. Using Text-File Databases
- 10.2. Using DBM Databases
- 10.3. Connecting to a SQL Database
- 10.4. Querying a SQL Database
- 10.5. Retrieving Rows Without a Loop
- 10.6. Modifying Data in a SQL Database
- 10.7. Repeating Queries Efficiently
- 10.8. Finding the Number of Rows Returned by a Query
- 10.9. Escaping Quotes
- 10.10. Logging Debugging Information and Errors
- 10.11. Assigning Unique ID Values Automatically
- 10.12. Building Queries Programmatically
- 10.13. Making Paginated Links for a Series of Records
- 10.14. Caching Queries and Results
- 10.15. Program: Storing a Threaded Message Board
-
11. Web Automation
- Introduction
- 11.1. Fetching a URL with the GET Method
- 11.2. Fetching a URL with the POST Method
- 11.3. Fetching a URL with Cookies
- 11.4. Fetching a URL with Headers
- 11.5. Fetching an HTTPS URL
- 11.6. Debugging the Raw HTTP Exchange
- 11.7. Marking Up a Web Page
- 11.8. Extracting Links from an HTML File
- 11.9. Converting ASCII to HTML
- 11.10. Converting HTML to ASCII
- 11.11. Removing HTML and PHP Tags
- 11.12. Using Smarty Templates
- 11.13. Parsing a Web Server Log File
- 11.14. Program: Finding Stale Links
- 11.15. Program: Finding Fresh Links
-
12. XML
- Introduction
- 12.1. Generating XML Manually
- 12.2. Generating XML with the DOM
- 12.3. Parsing XML with the DOM
- 12.4. Parsing XML with SAX
- 12.5. Transforming XML with XSLT
- 12.6. Sending XML-RPC Requests
- 12.7. Receiving XML-RPC Requests
- 12.8. Sending SOAP Requests
- 12.9. Receiving SOAP Requests
- 12.10. Exchanging Data with WDDX
- 12.11. Reading RSS Feeds
-
13. Regular Expressions
- Introduction
- 13.1. Switching From ereg to preg
- 13.2. Matching Words
- 13.3. Finding the nth Occurrence of a Match
- 13.4. Choosing Greedy or Nongreedy Matches
- 13.5. Matching a Valid Email Address
- 13.6. Finding All Lines in a File That Match a Pattern
- 13.7. Capturing Text Inside HTML Tags
- 13.8. Escaping Special Characters in a Regular Expression
- 13.9. Reading Records with a Pattern Separator
-
14. Encryption and Security
- Introduction
- 14.1. Keeping Passwords Out of Your Site Files
- 14.2. Obscuring Data with Encoding
- 14.3. Verifying Data with Hashes
- 14.4. Storing Passwords
- 14.5. Checking Password Strength
- 14.6. Dealing with Lost Passwords
- 14.7. Encrypting and Decrypting Data
- 14.8. Storing Encrypted Data in a File or Database
- 14.9. Sharing Encrypted Data with Another Web Site
- 14.10. Detecting SSL
- 14.11. Encrypting Email with GPG
-
15. Graphics
- Introduction
- 15.1. Drawing Lines, Rectangles, and Polygons
- 15.2. Drawing Arcs, Ellipses, and Circles
- 15.3. Drawing with Patterned Lines
- 15.4. Drawing Text
- 15.5. Drawing Centered Text
- 15.6. Building Dynamic Images
- 15.7. Getting and Setting a Transparent Color
- 15.8. Serving Images Securely
- 15.9. Program: Generating Bar Charts from Poll Results
-
16. Internationalization and Localization
- Introduction
- 16.1. Listing Available Locales
- 16.2. Using a Particular Locale
- 16.3. Setting the Default Locale
- 16.4. Localizing Text Messages
- 16.5. Localizing Dates and Times
- 16.6. Localizing Currency Values
- 16.7. Localizing Images
- 16.8. Localizing Included Files
- 16.9. Managing Localization Resources
- 16.10. Using gettext
- 16.11. Reading or Writing Unicode Characters
-
17. Internet Services
- Introduction
- 17.1. Sending Mail
- 17.2. Sending MIME Mail
- 17.3. Reading Mail with IMAP or POP3
- 17.4. Posting Messages to Usenet Newsgroups
- 17.5. Reading Usenet News Messages
- 17.6. Getting and Putting Files with FTP
- 17.7. Looking Up Addresses with LDAP
- 17.8. Using LDAP for User Authentication
- 17.9. Performing DNS Lookups
- 17.10. Checking if a Host Is Alive
- 17.11. Getting Information About a Domain Name
-
18. Files
- Introduction
- 18.1. Creating or Opening a Local File
- 18.2. Creating a Temporary File
- 18.3. Opening a Remote File
- 18.4. Reading from Standard Input
- 18.5. Reading a File into a String
- 18.6. Counting Lines, Paragraphs, or Records in a File
- 18.7. Processing Every Word in a File
- 18.8. Reading a Particular Line in a File
- 18.9. Processing a File Backward by Line or Paragraph
- 18.10. Picking a Random Line from a File
- 18.11. Randomizing All Lines in a File
- 18.12. Processing Variable Length Text Fields
- 18.13. Reading Configuration Files
- 18.14. Reading from or Writing to a Specific Location in a File
- 18.15. Removing the Last Line of a File
- 18.16. Modifying a File in Place Without a Temporary File
- 18.17. Flushing Output to a File
- 18.18. Writing to Standard Output
- 18.19. Writing to Many Filehandles Simultaneously
- 18.20. Escaping Shell Metacharacters
- 18.21. Passing Input to a Program
- 18.22. Reading Standard Output from a Program
- 18.23. Reading Standard Error from a Program
- 18.24. Locking a File
- 18.25. Reading and Writing Compressed Files
- 18.26. Program: Unzip
-
19. Directories
- Introduction
- 19.1. Getting and Setting File Timestamps
- 19.2. Getting File Information
- 19.3. Changing File Permissions or Ownership
- 19.4. Splitting a Filename into Its Component Parts
- 19.5. Deleting a File
- 19.6. Copying or Moving a File
- 19.7. Processing All Files in a Directory Recursively
- 19.8. Getting a List of Filenames Matching a Pattern
- 19.9. Processing All Files in a Directory
- 19.10. Making New Directories
- 19.11. Removing a Directory and Its Contents
- 19.12. Program: Web Server Directory Listing
- 19.13. Program: Site Search
-
20. Client-Side PHP
- Introduction
- 20.1. Parsing Program Arguments
- 20.2. Parsing Program Arguments with getopt
- 20.3. Reading from the Keyboard
- 20.4. Reading Passwords
- 20.5. Displaying a GUI Widget in a Window
- 20.6. Displaying Multiple GUI Widgets in a Window
- 20.7. Responding to User Actions
- 20.8. Displaying Menus
- 20.9. Program: Command Shell
- 20.10. Program: Displaying Weather Conditions
- 21. PEAR
- Index
- Colophon
Product information
- Title: PHP Cookbook
- Author(s):
- Release date: November 2002
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781565926813
You might also like
book
PHP Cookbook
If you're a PHP developer looking for proven solutions to common problems, this cookbook provides code …
book
PHP Cookbook, 2nd Edition
When it comes to creating dynamic web sites, the open source PHP language is red-hot property: …
book
PHP Cookbook, 3rd Edition
Want to understand a certain PHP programming technique? Or learn how to accomplish a particular task? …
book
Programming PHP, 4th Edition
Why is PHP the most widely used programming language on the web? This updated edition teaches …