Chapter 1. The Good Parts

This book has been a rather long time in the making. I have been using PHP for many years now, and have grown to love it more and more for its simplistic approach to programming, its flexibility, and its power. Of all the programming languages I have used throughout my over 20-year career, PHP is my favorite, hands down. PHP has grown from a small set of functions to a very large volume of functions, external interfaces, and add-on tools. Some programmers may be overwhelmed by its apparent vastness, but I hope to show you in this book that most of the PHP world can indeed be of great use. In a relatively short amount of pages, you will be shown all the best areas of the PHP development environment. By the time you get to the last page, you will have a better understanding of how powerful that environment is in the web development sphere.

Why PHP?

With so many programming books on the market these days—and so many PHP books—you might wonder what another book could accomplish. PHP is a widely used language and has experienced much growth in recent years in the enterprise market. Web environments like Facebook, Flickr, portions of Yahoo!, and Wikipedia all use PHP in a significant way, and web content management systems like Drupal, Joomla, and WordPress are also powered by PHP. IBM is also showing a lot of interest in integrating its technologies with PHP. For these reasons, it makes sense for the community to assist beginning and intermediate programmers in becoming familiar with all the best areas of this language.

A Brief History of PHP

Let’s start with a brief history of the language. Personal Home Page (PHP), initially known as PHP Tools, was launched in June 1995 by Rasmus Lerdorf. It was originally launched as open source software and remains so to this day. Database integration was implemented in version 2.0 in 1996, and the product has grown by leaps and bounds ever since. Its worldwide use is higher than any other web development language. As of this writing, the latest version of PHP is 5.3, which was released on June 30, 2009.

PHP’s Place in the World

PHP is one of the most widely used programming languages in the world. To think that it has grown this much in such a short period of time is quite impressive; in just 15 years or so, it has grown to be one of the major players in the web development world. In the last several years, many members of the PHP community have been debating whether the language is enterprise ready: can it be trusted to handle the big projects and weights? Given the recent focus on PHP from companies like IBM and Microsoft, and the fact that it powers the largest websites in the world (Facebook and Yahoo!), one could argue that it is already in the enterprise. This debate will be resolved over time, but with version 5.3 just recently having been released, it is a safe bet to say that if it isn’t, it very soon will be.

What Is PHP?

So what is PHP anyway? It is a scripting language, mostly used on the server side, that can be employed to generate Hypertext Markup Language (HTML) information dynamically. PHP is connected to a web server, generally Apache or Internet Information Server (IIS), and once it has finished generating proper HTML, it sends its creation back to the web server for delivery to the requesting client.

I say “mostly used” on the server side because you can use PHP in many other areas, including command line, desktop PC, and client server environments, just to name a few. However, it is most commonly used in the web server environment.

PHP developers can also integrate PHP with one of many different database tools like MySQL, SQLite, PostgreSQL, DB2, MS SQL, ORACLE, and so on, to make their created content as dynamic as possible. In reality, what is produced is still a static HTML file, but it is produced on the fly and therefore seems to be dynamic. Actually, one could argue that since the content is dynamically drawn out of a database or some other data source, PHP is in fact creating dynamic content.

What Has Been Accomplished with PHP?

Now, saying all these things about PHP and not having any proof would be untoward for sure, so let’s take a quick highlight tour of what has been built and accomplished with PHP. Some of the major and most popular web locations in the world are powered at some level by PHP. Table 1-1 includes a short list of popular websites, their Uniform Resource Locators (URLs), and a brief description of what each does.

Table 1-1. Sampling of major websites that use PHP

Website name

Description

URL

Facebook

Social networking

http://www.facebook.com

Flickr

Photograph sharing

http://www.flickr.com

Wikipedia

Online collaborative encyclopedia

http://www.wikipedia.org

SugarCRM

Customer relationship management tool

http://www.sugarcrm.com

Dotproject

Project management tool

http://www.dotproject.org

Drupal

Website construction template engine

http://drupal.org

Interspire

Newsletter and email marketing product

http://www.interspire.com

This is only the proverbial tip of the iceberg, and is not in any way meant to be an exhaustive list; it is simply a short list of examples of what has been built with PHP. If you have been to any of these websites, you can see what this powerful language can accomplish.

Basic PHP Setup

By now you might be anxious to try PHP out for yourself, so we’ll go through a quick installation discussion here and have you saying, “Hello, world” in no time.

The basic method of PHP development is to build PHP code on top of web server software like Apache or IIS. There is a “stack” of software that is generally used for a fully functional development environment: either LAMP or WAMP. LAMP stands for Linux/Apache/MySQL/PHP, but there are variations to this, as one would expect. You could be using PostgreSQL instead of MySQL for the database and therefore the acronym would be LAPP, but you get the idea. The other acronym—WAMP—stands for Windows/Apache/MySQL/PHP.

Note

Typically, the OS has no real bearing on the functionality of the written code. PHP written in the Windows environment will certainly operate just as well on a Linux box and vice versa. The only thing to be cautious of is if you are doing OS-level commands like CHMOD (for changing file permissions) or CHOWN (for changing file ownerships) in Linux and want to do the same in a different OS. Just be sure to test your code well in this, and all, instances.

Since there are so many different platforms and components to setting up a full PHP development environment, we won’t go into detail on how to establish that environment here. Be sure to go to http://www.php.net/downloads.php for a full listing of the latest stable releases for the many and varied platforms. There are also some all-in-one installation packages for Windows; one is called XAMPP (X for cross-platform, A for Apache, M for MySQL, P for PHP, and P for Perl), which can be found at http://www.apachefriends.org/en/xampp-windows.html. After you have the package for the appropriate platform, look for a file called install.txt among the downloaded files for a setup guide.

Once you have PHP installed, you should be able to run a small script that will interpret your php.ini settings file and show you all your directives and setting values. The code for doing this is one line, like so:

<?php phpinfo() ; ?>

The way to start and stop PHP content is with the <?php text sequence and the ?> text sequence, respectively, but more on that in the next chapter. For now, save this code in your web root folder (usually www or htdocs) as phpinfo.php. When you enter http://localhost/phpinfo.php in the browser, the output should resemble Figure 1-1.

Result of phpinfo() function
Figure 1-1. Result of phpinfo() function

Take some time to review these settings, and don’t worry if you are not sure what most of them are; simply having a screen that looks like Figure 1-1 is proof enough that PHP is properly installed and being served through your localhost web server.

Note

Localhost is the web address prefix for all the PHP code you write in your local computer environment. If you have code running off of a remote server, you either reference it with a proper web address or a specific IP number.

Now let’s write a little code here to do the proverbial worldwide greeting. Open a file called HelloOutThere.php under the document root—typically, this is /var/www/ in Linux or ../apache2/htdocs in Windows—and enter the following code:

<?php  echo "Hello, is there anybody out there?" ; ?>

Then enter the following into the browser’s address field: http://localhost/HelloOutThere.php. The result should be a browser page similar to Figure 1-2.

HelloOutThere.php example browser output
Figure 1-2. HelloOutThere.php example browser output

What we are telling the web server to do here is to repeat (echo) something into the browser’s display area. With the echo command, we can send a string of text or, as you will see later in this book, almost anything within the web context. That’s all there is to it. You have just created your first PHP web page.

Get PHP: The Good Parts 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.