Php Developer

A.3 Installing PEAR

Many PEAR modules, such as the DB module discussed in Chapter 7, make your PHP programming life easier. They are high-quality code libraries that help you do all sorts of common tasks in PHP programs such as interacting with a database or generating an HTML form. I recommend always having the PEAR libraries available.

Generating a Recently Viewed Document Index

How many times have you returned to a Web site, wondering where exactly to find that great PHP tutorial that you nevertheless forgot to bookmark Wouldn't it be nice if the Web site were able to remember which articles you read, and present you with a list whenever requested This example demonstrates such a feature.

Who Should Read This Book

This book is aimed at intermediate and advanced Web designers looking to make the leap into server-side programming. You'll be expected to be comfortable with simple HTML, as I'll make use of it without much in the way of explanation. No knowledge of JavaScript is assumed or required, but if you do know JavaScript, you'll find it will make learning PHP a breeze, since the languages are quite similar.

Forms and Navigational Cues

You can throw about technical terms such as relational database, Web Services, session handling, and LDAP, but when it comes down to it, you started learning PHP because you wanted to build cool, interactive Web sites. After all, one of the Web's most alluring aspects is that it's a two-way media the Web not only enables you to publish information, but also offers a highly effective means for interaction. This chapter formally introduces one of the most common ways in which you can use PHP to interact with the user Web forms. In addition, you'll learn a few commonplace site-design strategies that will help the user to better engage with your site and even recall key aspects of your site structure more easily. This chapter presents three such strategies, referred to as navigational cues, including user-friendly URLs, breadcrumb trails, and custom error pages.

Advanced OOP Features

Chapter 6 introduced the fundamentals of object-oriented PHP programming. This chapter builds on that foundation by introducing several of the more advanced OOP features that you should consider once you have mastered the basics. Specifically, this chapter introduces the following five features

Using Variables and Input

In chapter 1, Exploring the PHP Environment, you learn the foundations of all PHP programming. If you have your environment installed, you're ready to write some PHP programs. Computer programs are ultimately about data. In this chapter you begin looking at the way programs store and manipulate data in variables. Specifically, you learn how to

System-Level Program Execution

Truly lazy programmers know how to make the most of their entire server environment when developing applications, which includes exploiting the functionality of the operating system, file system, installed program base, and programming languages whenever necessary. In this section, you'll learn how PHP can interact with the operating system to call both OS-level programs and third-party installed applications. Done properly, it adds a whole new level of functionality to your PHP programming repertoire. Done poorly, it can be catastrophic not only to your application but also to your server's data integrity. That said, before delving into this powerful feature, take a moment to consider the topic of sanitizing user input before passing it to the shell level.

Installing and Updating PEAR

PEAR has become such an important aspect of efficient PHP programming that a stable release has been included with the distribution since version 4.3.0. Therefore, if you're running this version or later, feel free to jump ahead and review the section Updating Pear. If you're running PHP version 4.2.X or earlier, in this section you'll learn how to install the PEAR Package Manager on both the Unix and Windows platforms. Because many readers run Web sites on a shared hosting provider, this section also explains how to take advantage of PEAR without running the Package Manager.

Advanced Database Concepts

I had many goals in writing this second edition of my PHP Advanced book. The primary aim is to demonstrate what I consider to be advanced PHP programming doing the things you already do but better, doing things tangentially related to PHP, and taking advantage of aspects of the language with which the average user may not be familiar. A second goal is to help solve some of the problems often put forth (to me or otherwise) in emails, forums, and newsgroups. This chapter addresses both goals equally.

Variable Scope Within Classes

PHP 5.0 supports new keywords for defining member variables called public, private, and protected. These let you develop objects along principles more like Java than like earlier PHP programming. If you don't use one of these keywords before your var statement (like private var), the default is public.

How to Save the Result of an Online Image Processing Task

One of the excellent features of PHP programming is its session capabilities. There are some functions in PHP that are used to implement a session for each user who visits a web page. These functions implement cookies internally even if the browser doesn't support it or if the users disable it themselves.

Advanced OOP Features Not Supported by PHP

Operator overloading The ability to assign additional meanings to operators based upon the type of data you're attempting to modify did not make the cut this time around. Based on discussions found in the PHP developer's mailing list, it is unlikely that this feature will ever be implemented.

How This Book Is Organized

The appendixes cover several topics of interest to the MySQL PHP developer. In them you can find installation and configuration instructions, quick reference guides to PHP and MySQL functions, a regular expressions overview, and guides to MySQL administration. In addition, you can find a few helpful resources, some snippets of code, and instructions on using the CD-ROM.

Dealing with Special Characters

When it comes to displaying large text blocks on a web page, a PHP developer must grapple with a number of issues. Special characters need to be protected, white space and line breaks must be preserved, and potentially malicious HTML code must be defanged. PHP comes with a number of functions designed to perform just these tasks.

Seven Questions for Any Prospective Hosting Provider

On the surface, most Web hosting providers offer a seemingly identical array of offerings, boasting absurd amounts of disk space, endless bandwidth, and impressive guaranteed server uptimes. Frankly, chances are that any respected hosting provider is going to meet and even surpass your expectations, not only in terms of its ability to meet the resource requirements of your Web site, but also in terms of its technical support services. However, as a PHP developer, there are several questions you should ask before settling upon a provider

The Need for Structured Code

From one perspective, the whole point of server-side languages like PHP is to reduce the sheer quantity of code that goes into your average Website. Consider that before the advent of server-side programming, a site with 1,000 jokes would typically consist of at least 1,000 files, each containing the HTML code to display one article in a Web browser. As a PHP developer, you could be forgiven for finding it hard to believe Websites were ever built that way. Why, then, do so many PHP developers fall into the trap of writing the same code over and over again when dealing with pieces of PHP code that are useful in several places on a site For example, to display a Top 10 Jokes listing on every page of a site, many PHP developers would copy the code to fetch that list from the database into each of the PHP files that make up the site

Advanced PHP Techniques

At the most basic level good programming is determined by whether or not an application or script works as intended. This is where the beginning programmer will leave things, and there is nothing wrong with that. However, the advanced programmer will work past that point, striving toward improved efficiency, reliability, security, and portability. This book teaches you how to develop the skills of an advanced PHP programmer. One thing the advanced PHP programmer does better than the beginner is learning to take advantage of more obscure or harder-to-comprehend features of the language. For example, while you probably already know how to use arrays, you may not have mastered multidimensional arrays creating them, sorting them, and so on. You have written your own functions by this point but may not understand how to use recursion and static variables. Issues like these will be discussed as well as other beyond-the-basics concepts, like the heredoc syntax and the printfQ sprintfQ family...

Using printf() and sprintf()

For most PHP programmers, the print() and echo() functions are all they need for printing text and variables. The advanced PHP programmer might occasionally use the more sophisticated printf() function. This function prints text but also has the ability to format the output. The PHP manual definition of this function is printf(string format , mixed arguments )

About the Technical Reviewer

LIGAYA TURMELLE is a full-time goddess, occasional PHP programmer, and obsessive world traveler. She lives in Guam with her husband and daughter and their two Belgian Malinois. Actively involved with the PHP community as a founding principal of phpwomen. org, administrator at codewalkers.com, roving reporter for the Developer Zone on Zend.com, PHP blogger, and long-time busy body of phpc on freenode, she hopes one day to actually meet the people she talks to. Ligaya Turmelle is a Zend Certified Engineer and MySQL Core certified. When not sitting at her computer staring at the screen, Ligaya can usually be found playing golf, scuba diving, snorkeling, kayaking, hiking, or just out playing with the dogs all over Guam.

2.4 Questions and Exercises

Because much of the work that PHP does has to do with Web browsers, the language has evolved a number of structures and mechanisms having to do with HTTP. As a PHP programmer, you should have a good understanding of these elements of the language. They are all the more important because other protocols, chiefly simple object access protocol (SOAP), covered in Chapter 5, ride (or can ride) on top of HTTP.

Modularizing a Web Site

In my experience, the arc of a programmer's development starts with writing one-page applications that do just a single thing. These turn into two-page tools, and eventually into multipage sites, using templates, sessions, and or cookies to tie them all together. For many programmers, though, the arc is actually a bell curve. After more and more experience, the seasoned PHP developer starts doing the same amount of work in fewer pages, like having the same script both display and handle a form. Or, conversely, the advanced PHP programmer may start making individual scripts that actually do less, by focusing each on a particular task. This is the premise behind modularizing a Web site.

Using Code Examples

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example Learning PHP and MySQL, Second Edition, by Michele E. Davis and Jon A. Phillips. Copyright 2007 Michele E. Davis and Jon A. Phillips, 978-0-596-51401-3.

Ntroduction

This book is aimed at designers, and specifically Flash designers, although my main aim is to teach you PHP in a friendly and designer-focused way. For that reason I've assumed, for the purpose of the case studies, that you'll know the basics of Flash and will be learning PHP to improve your Flash sites. But whether you're a Flash master or a novice, I'll nevertheless fully explain the Flash that we use throughout the book.

About the Reviewers

Alexander Marcus Turek was born on June 2nd, 1984 in Dusseldorf, the capital of the German province Northrhine-Westphalia. Currently, he's studying Information Engineering and Management at the University of Karlsruhe, but his origin is Mulheim an der Ruhr, the home of his family. He first got in touch with the Web in 1998, when he won a 28.8k modem at the CeBit Home in Hannover, Germany. A few months later, he learned HTML and started his first Web project, a German game patch archive called Rabus' Update Site, which he renamed to bugfixes.info, when the .info domains became available. In the meantime, he switched from static HTML to PHP in order to be able to manage the growing archive more efficiently. He kept on learning PHP when trying to extend the portal.

Colophon

The image on the cover of Learning PHP and MySQL is of kookaburra birds (Dacelo). This laughing bird is indigenous to the eastern woodland parts of Australia, and it derives its name from its distinctive call. Similar to a loud, howling laugh, it sounds as if the bird is saying koo koo koo ka ka ka. It typically makes this call at dawn and again in the early evening to mark its territory. The call is also used as a greeting and can get quite loud if groups of the birds meet each other and begin engaging in conversations.

Summary

This chapter covered quite a bit of material, beginning with an overview of several date and time functions that appear almost daily in typical PHP programming tasks. Next up was a journey into the ancient art of Date Fu, where you learned how to combine the capabilities of these functions to carry out useful chronological tasks. You also read about the useful Calendar PEAR package, where you learned how to create grid-based calendars and validation and navigation mechanisms. Finally, an introduction to PHP 5.1's object-oriented date-manipulation features was provided.

Using PEAR

PEAR is a framework and distribution system for reusable PHP components, creating a collection of add-on functionalities for PHP development. There are many modules available to handle everything from session management to shopping cart functionality. Categories of modules that are currently available are listed in Table 9-1.

Introducing PHP

In many ways the PHP language is representative of the stereotypical open source project, created to meet a developer's otherwise unmet needs and refined over time to meet the needs of its growing community. As a budding PHP developer, it's important you possess some insight into how the language has progressed, as it will help you to understand the language's strengths, and to some extent the reasoning behind its occasional idiosyncrasies.

Caching Feeds

Everyone agrees that XML signifies an enormous leap forward in data management and application interoperability. Yet how come it's so darned hard to parse Although powerful parsing solutions are readily available, DOM, SAX, and XSLT to name a few, each presents a learning curve that is just steep enough to cause considerable gnashing of the teeth among those users interested in taking advantage of XML's practicalities without an impractical time investment. Leave it to an enterprising PHP developer (namely, Sterling Hughes) to devise a graceful solution. SimpleXML offers users a

About This Book

Simply put, I've tried to make this book's content accessible and useful for every PHP programmer out there. As I suggest in the introductory paragraphs, I believe that advanced PHP is mostly a matter of extended topics. You already possess all the basic knowledge you retrieve database query results in your sleep but want to go further. This may mean learning object-oriented programming (OOP), using PEAR (PHP Extension and Application Repository), incorporating Ajax (Asynchronous JavaScript and XML) into a site, or improving upon aspects of your existing skill set. My definition of advanced PHP programming covers three loosely grouped skills This book can be divided into three sections, corresponding to those skills. The first five chapters cover advanced PHP knowledge in general programming techniques, Web applications, databases, security, and e-com-merce. They all cover information that the average PHP programmer may not be familiar with but should be able to comprehend, providing...