PHP Solutions: Dynamic Web Design Made Easy, Third Edition (2014)
When the first edition of PHP Solutions was published, I was concerned that the subtitle, Dynamic Web Design Made Easy, sounded overambitious. Even with this third edition, it still makes me a little apprehensive about unduly raising readers’ expectations. PHP is not difficult, but nor is it like an instant cake mix: just add water and stir. Every website is different, so it’s impossible to grab a script, paste it into a webpage, and expect it to work. My aim was to help web designers with little or no knowledge of programming gain the confidence to dive into the code and adjust it to their own requirements.
The fact that the book has remained so popular since it was first published in 2006 suggests that many readers took up the challenge. Members of Boston PHP did so in large numbers when they adopted the second edition as the text for three series of PHP Percolate, a virtual self-study group for beginners. Hundreds signed up to study the book one chapter a week. It worked for them, so I hope it will work just as well for you.
What’s New in this Edition
One useful piece of feedback from PHP Percolate participants and other readers was disappointment when I glossed over a section of advanced code, explaining only what it did rather than how it worked. That omission has been corrected in this edition. Occasionally, I point out that you might want to skip the detailed explanation, but it’s there if you’re intrigued by how a technique works. As a result, the reference section of Chapter 3 has been expanded to include such esoteric delights as variable variables. No, it’s not a typo; “variable variable” is a genuine concept in PHP. It’s also quite useful.
This edition brings the content up to date with PHP 5.6, which was released in August 2014. Because hosting companies are often slow to upgrade the version of PHP that they offer, I’ve made PHP 5.4 the minimum version for the code used in this book. PHP 5.4 made some important changes, introducing a simplified array syntax and dropping support for safe mode and “magic quotes.” As well as bringing the code up to date, I’ve revised every chapter, going through it line by line, clarifying explanations. I’ve also eliminated a number of errors—without, I hope, introducing new ones.
The biggest changes are to the custom classes for uploading files and creating image thumbnails in Chapters 6 and 8. They now use namespaces to avoid naming clashes with other third-party code. More important, the class definitions have been extensively rewritten to make them more efficient. Another significant change is the use of the new password hashing functions in Chapters 9 and 17. These functions weren’t introduced until PHP 5.5, but you can emulate them in PHP 5.4 by including the password_compat library in your scripts. Details of how to obtain the library, which consists of a single file, can be found in Chapter 9.
The chapters on working with a database have been reorganized to make them easier to follow. I’ve also strengthened the explanation of prepared statements, using both MySQL Improved (MySQLi) and the database-neutral PHP Data Objects (PDO). Some Linux distributions now install MariaDB as a drop-in replacement for MySQL. To avoid unnecessary repetition, I normally refer only to MySQL, but all the PHP solutions in this book work equally well with MariaDB.
How This Book Is Organized
Each chapter takes you through a series of stages in a single project, with each stage building on the previous one. By working through each chapter, you get the full picture of how everything fits together. You can later refer to the individual stages to refresh your memory about a particular technique. Although this isn’t a reference book, Chapter 3 is a primer on PHP syntax, and some chapters contain short reference sections—notably Chapter 7 (reading from and writing to files), Chapter 9 (sessions), Chapter 10 (data types in MySQL/MariaDB), Chapter 11 (PHP prepared statements), Chapter 13 (the four essential SQL commands), and Chapter 14 (working with dates and times).
So, how easy is easy? I have done my best to ease your path, but there is no magic potion. It requires some effort on your part. Don’t attempt to do everything at once. Add dynamic features to your site a few at a time. Get to understand how they work, and your efforts will be amply rewarded. Adding PHP and MySQL/MariaDB to your skills will enable you to build websites that offer much richer content and an interactive user experience.
Using the Example Files
All the files necessary for working through this book can be downloaded from the Apress website at www.apress.com/9781484206362. Make sure you select the download link for PHP Solutions: Dynamic Web Design Made Easy, Third Edition. The code is different from the first two editions.
Set up a PHP development environment, as described in Chapter 2. Unzip the files and copy the phpsols folder and all its contents into your web server’s document root. The code for each chapter is in a folder named after the chapter: ch01, ch02, and so on. Follow the instructions in each PHP solution, and copy the relevant files to the site root or the work folder indicated.
Where a page undergoes several changes during a chapter, I have numbered the different versions like this: index_01.php, index_02.php, and so on. When copying a file that has a number, remove the underscore and number from the filename, so index_01.php becomesindex.php. If you are using a program like Dreamweaver that prompts you to update links when moving files from one folder to another, do not update them. The links in the files are designed to pick up the right images and style sheets when located in the target folder. I have done this so you can use a file comparison utility to check your files against mine.
If you don’t have a file comparison utility, I strongly urge you to install one. It will save you hours of head scratching when trying to spot the difference between your version and mine. A missing semicolon or mistyped variable can be hard to spot in dozens of lines of code. Windows users can download WinMerge for free from http://winmerge.org/. I use Beyond Compare (www.scootersoftware.com), which is now available for Windows, Mac OS X, and Linux. It’s not free but is excellent and reasonably priced. BBEdit on a Mac includes a file comparison utility. Alternatively, use the file comparison feature in TextWrangler, which can be downloaded free from www.barebones.com/products/textwrangler/.
To keep this book as clear and easy to follow as possible, the following text conventions are used throughout:
· Important words or concepts are normally highlighted on the first appearance in bold type.
· Code is presented in fixed-width font.
· New or changed code is normally presented in bold fixed-width font.
· Pseudo-code and variable input are written in italic fixed-width font.
· Menu commands are written in the form Menu Submenu Submenu.
· Where I want to draw your attention to something, I’ve highlighted it, like this:
Ahem, don’t say I didn’t warn you.