Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Introduction

Introduction

My first encounter with PHP came about 10 years ago. By that time, I already had plenty of experience developing websites. I had started out writing HTML in a text editor before settling on Dreamweaver as my favorite authoring tool. A new project involved publishing more than 30 articles a day. It was a subscription service, so the site needed to be password-protected and searchable. An ordinary website wouldn’t do. That’s when PHP came to the rescue.

PHP makes communication with a database a breeze, so content can be stored in the database, making it searchable. Instead of creating a new page for every article, pages are populated dynamically with the requested items. You can also password-protect the administrative or members-only area of a site. PHP does a lot more: It can send email, upload files, and attach files to emails—all of which you’ll learn how to do in this book. PHP is also the driving force behind the three most popular content management systems: Drupal, Joomla!, and WordPress.

So, where does Dreamweaver come into the picture? Dreamweaver has supported PHP to some degree since 2002, mainly through server behaviors, which automatically generate PHP code for some basic tasks. But the level of support has taken a quantum leap forward in Dreamweaver CS5. The server behaviors are still there (see Lesson 6), but they take a back seat.

The big changes lie in code hinting, embedded PHP documentation (including examples), autocompletion of variables, automatic discovery of dynamically related files, and—perhaps best of all—the ability to view and navigate through PHP pages without leaving the Document window. As a result, it’s now possible to style WordPress, Joomla!, and Drupal in Dreamweaver CS5 without the need to generate static pages. These changes are described in detail in Lesson 1, but in a nutshell they should appeal to designers and developers alike.

PHP’s popularity springs from being easy to learn. You can achieve practical results very quickly. Of course, like any skill, becoming an expert takes time and practice. The new PHP features in Dreamweaver CS5 not only help the learning process, but you’ll find them even more useful as you gain experience. Dreamweaver is my preferred choice for designing the look of a website and organizing files, but I was beginning to use dedicated PHP authoring tools for the dynamic aspects of development. Dreamweaver CS5 has changed all that. I now have the best of both worlds in the same program.

Who This Book Is for

This is a “beyond the basics” book, so you should already have a solid understanding of how a website is built. You should also have a good understanding of HTML, because PHP code needs to be embedded in the underlying structure of a page to display the dynamic output. It’s not necessary to know every tag and attribute, but if you don’t know the difference between a <ul> and an <li> tag, you’ll be lost. All the example files and exercises are styled with CSS, but design is not the focus of this book. You don’t need to understand CSS to work through the lessons, but your web development skills would certainly be the better for it. You’ll also find it makes it easier to follow Lesson 4, where you create a new WordPress theme.

You don’t need prior knowledge of PHP. This book doesn’t teach PHP in a formal manner, but Lesson 3 provides a crash course in how to write PHP, and Lesson 5 teaches the basics of database design using MySQL, the most popular open source database.

If you already know some PHP, all the better. This book moves at a fairly rapid pace. Lessons 712 make extensive use of the Zend Framework, a powerful library of PHP components that take a lot of hard work out of creating dynamic sites. Lesson 12 also uses the jQuery JavaScript framework. Again, you don’t need prior knowledge of jQuery or JavaScript, but it will certainly help.

How to Use This Book

Time is precious, so you probably want to jump straight to the solution for your current problem. If you have considerable PHP experience, that approach might work. However, the majority of readers should start with Lesson 1 and work through each one in sequence because each lesson builds on the previous one. If you skip ahead, you’re likely to miss a vital explanation and will need to backtrack anyway.

The “Approximate Time” at the beginning of each lesson is simply an estimate of the time it will take to work through the exercises. Don’t regard it as a challenge, and don’t feel downcast if you take much longer. Each lesson is packed with information. Take time to absorb it, and break the lesson into smaller chunks to match your own pace.

Most lessons contain reference sections followed by hands-on exercises. Each step explains not only what to do, but also why you’re doing it. The idea is to help you think about how you could apply the same techniques to your own projects. This isn’t a point-and-click book, but instead is one that aims to stimulate your problem-solving abilities. The more you think, the more you’re likely to get out of it.

Accompanying files

The accompanying CD contains all the files necessary to complete the exercises in this book. The only exceptions are the PHP/MySQL development environments described in Lesson 2 and the LightBox Gallery Widget in Lesson 12. PHP and MySQL are updated frequently, so it makes more sense to get the most recent versions from the source. In the case of the LightBox Gallery Widget, one object of the exercise is to show you how to install the Adobe Widget Browser and download widgets from the Adobe Exchange.

Lesson 2 describes how to set up the Dreamweaver site to work through the exercises in this book. The files for each lesson are in folders named lesson01, lesson02, and so on. There are no files for Lesson 13. For each lesson that contains exercises, there are normally three subfolders: completed, start, and workfiles. The workfiles folder is deliberately left empty; it’s where you should create and save the files for the lesson’s exercises. If you follow this structure, the exercise files will use the common style sheets that are stored in the styles folder.

To save time, many exercises have partially completed pages, which you should copy from the start folder to the workfiles folder for that lesson. The completed folder contains copies of the exercise files shown at various stages of completion.

In Lessons 10 and 11, you should create a folder called cms in the site root. The cms_complete folder contains a full working copy of the completed project.

Note

The files were created on a Windows computer but are fully compatible with Mac OS X. However, the path in library.php needs to be adjusted to match the location of the Zend Framework files. See Lesson 7 for details.


Windows/Mac differences

The few Dreamweaver CS5 and PHP differences between Windows and Mac OS X have been pointed out at relevant places in the book.

Keyboard shortcuts are given in the order Windows/Mac, but in the rare cases where there is no Mac equivalent, this has been pointed out. On some Mac keyboards, the Opt(ion) key is labeled Alt. On a UK Mac keyboard, use Alt+3 to type the hash symbol (#).

Using a multi-button mouse with a Mac is now so common that the instructions refer only to right-click. If you prefer a single-button mouse, use Ctrl-click.

Code portability

One of the pleasures of working with PHP is that it’s platform-neutral. All the PHP code in this book works equally well on Windows, Mac OS X, and Linux. However, it’s important to realize that different versions of PHP and MySQL have different functionality. Also, server administrators have the ability to turn off certain features. To use this book, your web server must be running PHP 5.2 and MySQL 4.1 or later. The code will not work with earlier versions.

Getting help

When you encounter a problem, the first person to look to for help is you. Did you skip a step or mistype the name of a variable or function? One of the quickest ways of finding an error is to use Dreamweaver’s File Compare feature (choose Help > Using Dreamweaver CS5 > Creating and Managing Files > Comparing files for differences) to compare your file with the version in the completed folder.

File Compare requires a third-party file comparison utility. If you don’t have one installed, WinMerge (http://winmerge.org) for Windows and TextWrangler (www.barebones.com/products/textwrangler/) for Mac OS X are both free.

If you can’t solve the problem on your own and a quick search on the Internet doesn’t produce the answer, post a question in the Adobe forums. The best one for PHP questions is the Dreamweaver Application Development forum at http://forums.adobe.com/community/dreamweaver/dreamweaver_development. I’m frequently there providing help, so you might even get an answer from me.

I also post updates and tutorials on my website at http://foundationphp.com/, and you can follow me on Twitter @foundationphp.

Every care has been taken to eliminate errors, but if you think you have found one, please email errata@peachpit.com with the details.

Layout conventions

The following text conventions are used throughout this book:

  • Boldface text. Words in bold text indicate input that you should type in a field or the name of a file you should create.

  • Boldface code. Code that is added or changes is displayed in boldface.

    if ($_POST) {
      if (empty($_POST['username']) || empty($_POST['password'])) {
        $failed = TRUE;
      } else {
        require_once('library.php');
  • Long code. Sometimes, code won’t fit on a single line on the printed page. Where this happens, an arrow indicates the continuation of a broken line like this:

    $result = $recaptcha->verify($_POST[‘recaptcha_challenge_field’], $_POST[‘recaptcha_response_field’]);
    
    					  
  • Italics. Text in italics is for emphasis or to introduce important concepts.

Let the Journey Begin

Above all, enjoy the experience that lies ahead. Even if you find working with code uncomfortable to begin with, PHP is not hard. Welcome to the ever-growing PHP community.