What Is in This Book

We don’t expect that you’ll sit down and read this book from cover to cover. (although we’ll be happy if you do!). PHP programmers are constantly faced with a wide variety of challenges on a wide range of subjects. Turn to the PHP Cookbook when you encounter a problem you need to solve. Each recipe is a self-contained explanation that gives you a head start towards finishing your task. When a recipe refers to topics outside its scope, it contains pointers to related recipes and other online and offline resources.

If you choose to read an entire chapter at once, that’s okay. The recipes generally flow from easy to hard, with example programs that “put it all together” at the end of many chapters. The chapter introduction provides an overview of the material covered in the chapter, including relevant background material, and points out a few highlighted recipes of special interest.

The book begins with four chapters about basic data types. Chapter 1 covers details like processing substrings, manipulating case, taking strings apart into smaller pieces, and parsing comma-separated data. Chapter 2 explains operations with floating-point numbers, random numbers, converting between bases, and number formatting. Chapter 3 shows you how to manipulate dates and times, format them, handle time zones and daylight saving time, and find time to microsecond precision. Chapter 4 covers array operations like iterating, merging, reversing, sorting, and extracting particular elements.

Next are three chapters that discuss program building blocks. Chapter 5 covers notable features of PHP’s variable handling, like default values, static variables, and producing string representations of complex data types. The recipes in Chapter 6 deal with using functions in PHP: processing arguments, passing and returning variables by reference, creating functions at runtime, and scoping variables. Chapter 7 covers PHP’s object-oriented capabilities, with recipes on using overloading and polymorphism, defining constructors, and cloning objects.

The heart of the book is five chapters devoted to topics that are central to web programming. Chapter 8 covers cookies, headers, authentication, configuration variables, and other fundamentals of web applications. Chapter 9 covers processing and validating form input, displaying multi-page forms, showing forms with error messages, and escaping special characters in user data. Chapter 10 explains the differences between text-file, DBM, and SQL databases and, using the PEAR DB database abstraction layer, shows how to assign unique ID values, retrieve rows, change data, escape quotes, and log debugging information. Chapter 11 focuses on retrieving URLs and processing HTML but also touches on using templates and parsing server access logs. Chapter 12 covers XML and related formats, including the DOM, SAX, XSLT, XML-RPC, and SOAP.

The next section of the book is a series of chapters on other features and extensions of PHP that provide a lot of useful functionality. These are recipes that help you build applications that are more robust, secure, user-friendly, and efficient. Chapter 13 covers regular expressions, including matching a valid email address, capturing text inside of HTML tags, and using greedy or non-greedy matching. Chapter 14 discusses encryption, including generating and storing passwords, sharing encrypted data with others, storing encrypted data in a file or database, and using SSL. Chapter 15 shows you how to create graphics, with recipes on drawing text, lines, polygons, and curves. Chapter 16 helps you make your applications globally friendly and includes recipes on using locales and localizing text, dates and times, currency values, and images. Chapter 17 discusses network-related tasks, like reading and sending email messages and newsgroup posts, using FTP and LDAP, and doing DNS and Whois lookups.

Chapter 18 and Chapter 19 cover the filesystem. Chapter 18 focuses on files: opening and closing them, using temporary files, locking file, sending compressed files, and processing the contents of files. Chapter 19 deals with directories and file metadata, with recipes on changing file permissions and ownership, moving or deleting a file, and processing all files in a directory.

Last, there are two chapters on topics that extend the reach of what PHP can do. Chapter 20 covers using PHP outside of web programming. Its recipes cover command-line topics like parsing program arguments and reading passwords, as well as topics related to building client-side GUI applications with PHP-GTK like displaying widgets, responding to user actions, and displaying menus. Chapter 21 covers PEAR, the PHP Extension and Application Repository. PEAR is a collection of PHP code that provides various functions and extensions to PHP. We use PEAR modules throughout the book and Chapter 21 shows you how to install and upgrade them.

Get PHP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.