Preface

Boring web sites are static. Interesting web sites are dynamic. That is, their content changes. A giant static HTML page listing the names, pictures, descriptions, and prices of all 1,000 products a company has for sale is hard to use and takes forever to load. A dynamic web product catalog that lets you search and filter those products so you see only the six items that meet your price and category criteria is more useful, faster, and much more likely to close a sale.

The PHP programming language makes it easy to build dynamic web sites. Whatever interactive excitement you want to create—such as a product catalog, a blog, a photo album, or an event calendar—PHP is up to the task. And after reading this book, you’ll be up to the task of building that dynamic web site, too.

Who This Book Is For

This book is for:

  • A hobbyist who wants to create an interactive web site for himself, his family, or a nonprofit organization.

  • A web site builder who wants to use the PHP setup provided by an ISP or hosting provider.

  • A small business owner who wants to put her company on the Web.

  • A page designer who wants to communicate better with her developer co-workers.

  • A JavaScript whiz who wants to build server-side programs that complement her client-side code.

  • A blogger or HTML jockey who wants to easily add dynamic features to her site.

  • A Perl, ASP, or ColdFusion programmer who wants to get up to speed with PHP.

  • Anybody who wants a straightforward, jargon-free introduction to one of the most popular programming languages for building an interactive web site.

PHP’s gentle learning curve and approachable syntax make it an ideal “gateway” language for the nontechnical web professional. Learning PHP 5 is aimed at both this interested, intelligent, but not necessarily technical individual as well as at programmers familiar with another language who want to learn PHP.

Aside from basic computer literacy (knowing how to type, moving files around, surfing the Web), the only assumption that this book makes about you is that you’re acquainted with HTML. You don’t need to be an HTML master, but you should be comfortable with the HTML tags that populate a basic web page such as <html>, <head>, <body>, <p>, <a>, and <br>. If you’re not familiar with HTML, read HTML & XHTML: The Definitive Guide, Fifth Edition, by Bill Kennedy and Chuck Musciano (O’Reilly).

Contents of This Book

This book is designed so that you start at the beginning and work through the chapters in order. For the most part, each chapter depends on material in the previous chapters. Chapter 2, through Chapter 12 and Appendix B, each end with exercises that test your understanding of the content in the chapter.

Chapter 1, provides some general background on PHP and how it interacts with your web browser and a web server. It also shows some PHP programs and what they do to give you an idea of what PHP programs look like. Especially if you’re new to programming or building dynamic web sites, it is important to read Chapter 1.

The next four chapters give you a grounding in the fundamentals of PHP. Before you can write great literature, you need to learn a little grammar and some vocabulary. That’s what these chapters are for. (Don’t worry—you’ll learn enough PHP grammar and vocabulary right away to start writing some short programs, if not great literature.) Chapter 2 shows you how to work with different kinds of data such as pieces of text and numbers. This is important because the web pages that your PHP programs generate are just big pieces of text. Chapter 3, describes the PHP commands with which your programs can make decisions. These decisions are at the heart of the “dynamic” in “dynamic web site.” The concepts in Chapter 3 are what you use, for example, to display only items in a product catalog that fall between two prices a user enters in a web form.

Chapter 4, introduces arrays, which are collections of a bunch of individual numbers or pieces of text. Many frequent activities in PHP programs, such as processing submitted web form parameters or examining information pulled out of a database, involve using arrays. As you write more complicated programs, you’ll find yourself wanting to repeat similar tasks. Functions, discussed in Chapter 5, help you reuse pieces of your programs.

The three chapters after that cover three essential tasks in building a dynamic web site: dealing with forms, databases, and users. Chapter 6, supplies the details on working with web forms. These are the primary way that users interact with your web site. Chapter 7, discusses databases. A database holds the information that your web site displays, such as a product catalog or event calendar. This chapter shows you how to make your PHP programs talk to a database. With the techniques in Chapter 8, your web site can do user-specific things such as display sensitive information to authorized people only or tell someone how many new message board posts have been created since she last logged in.

Then, the next three chapters examine three other areas you’re likely to encounter when building your web site. Chapter 9, highlights the steps you need to take, for example, to display a monthly calendar or to allow users to input a date or time from a web form. Chapter 10, describes the PHP commands for interacting with files on your own computer or elsewhere on the Internet. Chapter 11, supplies the basics for dealing with XML documents in your PHP programs, whether you need to generate one for another program to consume or you’ve been provided with one to use in your own program.

Chapter 12 and Chapter 13 each stand on their own. Chapter 12, furnishes some approaches for understanding the error messages that the PHP interpreter generates and hunting down problems in your programs. While it partially depends on earlier material, it may be worthwhile to skip ahead and peruse Chapter 12 as you’re working through the book.

Chapter 13 serves a taste of many additional capabilities of PHP, such as generating images, running code written in other languages, and making Flash movies. After you’ve gotten comfortable with the core PHP concepts explained in Chapter 1 through Chapter 12, visit Chapter 13 for lots of new things to learn.

The three appendixes provide supplementary material. To run PHP programs, you need to have a copy of the PHP interpreter installed on your computer (or have an account with a web-hosting provider that supports PHP). Appendix A, helps you get up and running, whether you are using Windows, OS X, or Linux.

Many text-processing tasks in PHP, such as validating submitted form parameters or parsing an HTML document, are made easier by using regular expressions, a powerful but initially inscrutable pattern matching syntax. Appendix B, explains the basics of regular expressions so that you can use them in your programs if you choose.

Last, Appendix C, contains the answers to all the exercises in the book. No peeking until you try the exercises!

Other Resources

The online annotated PHP Manual (http://www.php.net/manual) is a great resource for exploring PHP’s extensive function library. Plenty of user-contributed comments offer helpful advice and sample code, too. Additionally, there are many PHP mailing lists covering installation, programming, extending PHP, and various other topics. You can learn about and subscribe to these mailing lists at http://www.php.net/mailing-lists.php. A read-only web interface to the mailing lists is at http://news.php.net. Also worth exploring is the PHP Presentation System archive at http://talks.php.net. This is a collection of presentations about PHP that have been delivered at various conferences.

After you’re comfortable with the material in this book, the following books about PHP are good next steps:

  • Programming PHP, by Rasmus Lerdorf and Kevin Tatroe (O’Reilly). A more detailed and technical look at how to write PHP programs. Includes information on generating graphics and PDFs.

  • PHP Cookbook, by David Sklar and Adam Trachtenberg (O’Reilly). A comprehensive collection of common PHP programming problems and their solutions.

  • Essential PHP Tools, by David Sklar (Apress). Examples and explanations about many popular PHP add-on libraries and modules including HTML_QuickForm, SOAP, and the Smarty templating system.

  • Upgrading to PHP 5, by Adam Trachtenberg (O’Reilly). A comprehensive look at the new features of PHP 5, including coverage of features for XML handling and object-oriented programming.

These books are helpful for learning about databases, SQL, and MySQL:

  • Web Database Applications with PHP & MySQL, by David Lane and Hugh E. Williams (O’Reilly). How to make PHP and MySQL sing in harmony to make a robust dynamic web site.

  • SQL in a Nutshell, by Kevin E. Kline (O’Reilly). The essentials you need to know to write SQL queries. Covers the SQL dialects used by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL.

  • MySQL Cookbook, by Paul DuBois (O’Reilly). A comprehensive collection of common MySQL tasks.

  • MySQL Reference Manual (http://dev.mysql.com/doc/mysql). The ultimate source for information about MySQL’s features and SQL dialect.

These books are helpful for learning about HTML and HTTP:

  • HTML & XHTML: The Definitive Guide, by Bill Kennedy and Chuck Musciano (O’Reilly). If you’ve got a question about HTML, this book answers it.

  • Dynamic HTML: The Definitive Reference, by Danny Goodman (O’Reilly). Full of useful information you need if you’re using JavaScript or Dynamic HTML as part of the web pages your PHP programs output.

  • HTTP Developer’s Handbook, by Chris Shiflett (Sams Publishing). With this book, you’ll better understand how your web browser and a web server communicate with each other.

These books are helpful for learning about security and cryptography:

  • Web Security, Privacy & Commerce, by Simson Garfinkel (O’Reilly). A readable and complete overview of the various aspects of web-related security and privacy.

  • Practical Unix & Internet Security, by Simson Garfinkel, Alan Schwartz, and Gene Spafford (O’Reilly). A classic exploration of all facets of computer security.

  • Applied Cryptography, by Bruce Schneier (John Wiley & Sons). The nitty gritty on how different cryptographic algorithms work and why.

These books are helpful for learning about supplementary topics that this book touches on like XML processing and regular expressions:

  • Learning XML, by Erik T. Ray (O’Reilly). Where to go for more in-depth information on XML than Chapter 11.

  • Learning XSLT, by Michael Fitzgerald (O’Reilly). Your guide to XML stylesheets and XSL transformations.

  • Mastering Regular Expressions, by Jeffrey E.F. Friedl (O’Reilly). After you’ve digested Appendix B, turn to this book for everything you ever wanted to know about regular expressions.

Conventions Used in This Book

The following programming and typesetting conventions are used in this book.

Programming Conventions

The code examples in this book are designed to work with PHP 5.0.0. They were tested with PHP 5.0.0RC2, which was the most up-to-date version of PHP 5 available at the time of publication. Almost all of the code in the book works with PHP 4.3 as well. The PHP 5-specific features discussed in the book are as follows:

  • Chapter 7: the mysqli functions

  • Chapter 10: the file_put_contents( ) function

  • Chapter 11: the SimpleXML module

  • Chapter 12: the E_STRICT error-reporting level

  • Chapter 13: some new features related to classes and objects, the advanced XML processing functions, the bundled SQLite database, and the Perl extension

Typographical Conventions

The following typographical conventions are used in this book:

Italic

Indicates new terms, example URLs, example email addresses, filenames, file extensions, pathnames, and directories.

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.

Constant width italic

Shows text that should be replaced with user-supplied values.

Tip

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

Typing some of the example programs in the book yourself is instructive when you are getting started. However, if your fingers get weary, you can download all of the code examples from http://www.oreilly.com/catalog/learnphp5.

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact the publisher for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Learning PHP 5 by David Sklar Copyright 2004 O’Reilly Media, Inc., 0-596-00560-1.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact the publisher at .

Comments and Questions

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway
North Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international or local)
(707) 829-0104 (fax)

There is a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/learnphp5

To comment or ask technical questions about this book, send email to:

Or you can contact the author directly via his web site:

http://www.sklar.com

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:

http://www.oreilly.com

Acknowledgments

This book is the end result of the hard work of many people. Thank you to:

  • The many programmers, testers, documentation writers, bug fixers, and other folks whose time, talent, and devotion have made PHP the first-class development platform that it is today. Without them, I’d have nothing to write about.

  • The Apple WWPM Hardware Placement Lab for the loan of an iBook, and to Adam Trachtenberg, George Schlossnagle, and Jeremy Zawodny for advice on some code examples.

  • My diligent reviewers: Griffin Cherry, Florence Leroy, Mark Oglia, and Stewart Ugelow. They caught plenty of mistakes, turned confusing explanations into clear ones, and otherwise made this book far better than it would have been without them.

  • Robert Romano, who turned my blocky diagrams and rustic pencil sketches into high-quality figures and illustrations.

  • Tatiana Diaz, who funneled all of my random questions to the right people, kept me on schedule, and ultimately made sure that whatever needed to get done, was done.

  • Nat Torkington, whose editorial guidance and helpful suggestions improved every part of the book. Without Nat’s feedback, this book would be twice as long and half as readable as it is.

For a better fate than wisdom, thank you also to Susannah, with whom I enjoy ignoring the syntax of things.

Get Learning PHP 5 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.