Chapter 1. Introduction

If you have never programmed before, but you are interested in developing PHP programs, this book is for you. It was originally written for a college course I've taught at Hendrix College in Arkansas, titled Computing and the Internet, where students learn about how the Web works. This course is meant for students who have no programming background and no intention of studying computing beyond the course.

(A lot of Web site developers get by using PHP scripts without having much understanding of how the PHP scripts work. This book is not meant to teach you how to get by with using PHP in this sense: The book is really intended for learning about programming. On the other hand, we also won't cover all of PHP: That's a job for the official manual. PHP is an extensive language, and we don't need to learn all of it to learn how to program.)

If you're an experienced programmer, you probably don't want to waste your time reading this. You'll probably find it easier just to refer to the official PHP manual, www.php.net/en/manual/.

1.1. About this book

This book is an open-source book, released under the Creative Commons Attribution-Share Alike license. Basically, that allows you to distribute and edit versions of this book, with some conditions. I think you'll agree that this approach to distributing the book is far preferable to the classical method of getting a publisher to publish and distribute the book, and then forcing readers to pay for the privilege of reading my words.

But there is a flip-side to this: Whereas publishers have the money to pay people to suggest improvements and to edit the book, I don't and must depend on my readers. If you find any errors, or if you find any parts particularly confusing, your e-mail to cburch at cburch dot c o m will be most welcome. If you find this book useful, then you can consider this e-mail message your payment for the book. (Please send e-mail about the text only. Unless you're a Hendrix student, I don't have time to field generic PHP questions.)

Throughout this book, we'll develop a simple Web site that combines all of the concepts that we'll study. This example Web site provides a forum, where users can post messages and view messages. We'll keep it very basic: Most notably, it won't even support threads or multiple fora. The final Web site we develop includes four pages. Many of them are presented multiple times, each time incorporating new concepts that we have seen.

page name versions presented purpose
user.php Section 7.4 View personal information about a single user.
view.php Section 8.2, Section 9.2, Section 14.3 View all messages currently in forum.
post.php Section 9.3, Section 14.3 Add a new message into the forum.
login.html Section 14.3 Log in to enter the forum.

1.2. Two sides of Web programming

Any Web communication has two basic sides of the conversation: You have the client, which is typically the Web browser, and which initiates the communication, and you have the server, a computer that is configured to wait for and serve any incoming communication requests.

Figure 1.1: The client-server communication model.

Web site development often involves writing programs telling computers what to do. But there are two very different types of Web programming, separated by which of the two sides is to execute the program: It may be that we want the server to execute the program, or it may be that we want the the Web browser to execute the program. Every Web program, then, can be called either server-side or client-side.

Both categories of programming are important in the modern Web, because each offers a distinct set of advantages and thus each is appropriate at different times. And in today's Web, they are completely separate, since they involve very different programming languages. Let us first look at their relative advantages, which dictate the instances where each applies.

Server-side advantages Client-side advantages
  • The server, since it is managed by the programmer, is a trusted computer, and so it can frequently be given permission to access trusted information — particularly a database. If we are selling something, for example, we would not want to allow browsers to execute programs to access the database, because we would then have to give the browsers permission to access the information, and somebody could write another program to access information we wouldn't want them to access.

  • There is only one server, which we can verify supports the programming technology we wish to use; this program generates HTML, which all Web browsers will be able to view. By contrast, client-side programs will normally not be compatible with some Web browsers, effectively cutting the Web site out from some audiences.

  • Client-side programs can respond immediately to user interaction, whereas server-side programs will require a significant delay each time user information is sent back to the server for processing.

  • Server-side programs send only HTML to the clients, restricting what they can do to the features that HTML provides. Client-side programming technologies often provide access to additional features, such as drawing animated graphics.

  • A single server often works with a multitude of users, but server-side programming makes it busier and may limit the number of users it can service.

You're more likely to have heard of client-side programming technologies: The most common are JavaScript and Flash. This is because many Web sites work only on browsers supporting the programming technology, so you as a Web surfer have to deal with making sure this is true. Moreover, Web sites with these technologies often appear much more dynamic than typical Web sites, since they tend to respond more directly to user input.

By contrast, server-side programs tend to be hidden from typical users, since the only people who need to worry about them are those who administer Web sites. And there's no reason to worry about being compatible with all the existing browsers, there are a multitude of popular choices, making all less well-known than they would otherwise be. Some of the most popular server-side programming languages are PHP, ASP, Perl, and Python.

In fact, many Web sites employ a combination of both client-side and server-side programming technologies, so that they can provide enhanced user interaction and access to database information. But for our purposes, we need to start somewhere, with just one programming language. And it makes sense to start with a language intended for server-side programming, since this type of programming is required for many Web sites. Thus, we'll study one of the most widely used server-side programming languages, PHP.

1.3. About PHP

PHP was initially developed by a Greenlandic programmer named Rasmus Lerdorf to facilitate developing his own personal Web page. After a while the package became sophisticated enough that he thought it was worth sharing with other people, so he released his set of programs, which he called Personal Home Page Tools, in mid-1995. (This was the time when the Web was getting its first widespread exposure and was becoming a mainstream phenomenon.)

At that time, the package was not yet something we would term a full-fledged programming language, but others found the package useful, and some joined Lerdorf to continue enhancing it. Two Israeli programmers, Andi Gutmans and Zeev Suraski, were interested in developing a language specifically for Web programming, and they thought PHP would be a good starting point. They joined Lerdorf and together the team developed the package into what we know today.

Along the way, PHP became appropriate for large Web sites, such as commercial sites, rather than just for personal home pages, so they began to refer to the package as simply PHP, which sounds like it could have once stood for Personal Home Page, but officially it stands for PHP: Hypertext Processor. Yes, the first P in PHP stands for PHP: They don't take this abbreviation idea too seriously.

PHP's structure is heavily influenced by the C programming language, a general-purpose language that has existed since 1978 and has been used to develop many large programs, including UNIX and Microsoft Windows. C's popularity led many subsequent languages to use its same general syntax, so that programmers familiar with C would quickly feel comfortable using the new language instead. Other C-based languages include C++, Java, Perl, JavaScript, and C#. Of course, all are fairly distinct languages, but since all of these languages have a similar core, you will find them fairly familiar after seeing PHP.

PHP development is continuing, as those on the PHP team refine the language to enhance its security, facilitate development of large-scale Web sites, and provide other new features. As of mid-2007, when this was written, the current official version of PHP is version 5, although version 6 is nearing completion. Version 4 is still in wide use, partially because people haven't gotten around to installing the newer versions — but also because established Web sites need to take issues like security and reliability very seriously, and version 4 is a more thoroughly tested software package.