Thursday, April 26, 2007

Introduction to PHP

The key to understanding something is to know how it got to where it is currently, and PHP is no different in that respect - it has a long history full of key design decisions and changes to the language. The history of web development is even longer - this section will cover both.

PHP - What is it?
Taken directly from PHP's home, PHP.net, "PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly."

This is generally a good definition of PHP. However, it does contain a lot of terms you may not be used to. Another way to think of PHP is a powerful, behind the scenes scripting language that your visitors won't see!

When someone visits your PHP webpage, your web server processes the PHP code. It then sees which parts it needs to show to visitors(content and pictures) and hides the other stuff(file operations, math calculations, etc.) then translates your PHP into HTML. After the translation into HTML, it sends the webpage to your visitor's web browser.

PHP - What's it do?
It is also helpful to think of PHP in terms of what it can do for you. PHP will allow you to:

  • Reduce the time to create large websites.
  • Create a customized user experience for visitors based on information that you have gathered from them.
  • Open up thousands of possibilities for online tools. Check out PHP - HotScripts for examples of the great things that are possible with PHP.
  • Allow creation of shopping carts for e-commerce websites.


  • PHP is cross-platform. It can be run on Windows, Linux, BSD, Mac OS X, and Solaris, as well as a variety of other platforms.
  • PHP is free. You can download the source code, use it, and even make changes to it without ever having to pay any licensing costs. You can even give away your own modified version of PHP. Note to critics: just because PHP is free, it does not mean you need to give your scripts away for free.
  • PHP is fast. In the majority of scripts beyond basic benchmarks, PHP will easily compete with both Perl and Python, and usually pull ahead of Microsoft's ASP.NET by about 10-15%. Add to that the fact that PHP code can be cached for execution, and
  • PHP's performance is first-class.
  • PHP is capable. There are thousands of pre-written functions to perform a wide variety of helpful tasks – handling databases of all sorts (MySQL, Oracle, MS SQL, PostgreSQL, and many others), file uploads, FTP, email, graphical interfaces, generating Flash movies, and more.
  • PHP is extendable. Writing your own extension to PHP is a common and easy way to implement speed-critical functionality, and PHP's extension API is a particularly rich and flexible system.
  • PHP is reliable. As an official Apache Foundation software project, PHP is brought to you by the same people that produce Apache, the world's most popular web server.
  • PHP is easy to debug. There are a number of debuggers, both commercial and freeware, that make debugging PHP a snap.
  • PHP is supported commercially. Two of the main contributors to PHP founded a company, Zend, to sell supporting products and technical support for the language, so there is no need to worry about PHP not being supported by a big company.
  • PHP is supported by the community. There are several very popular PHP web-sites such as www.phpbuilder.com that provide user-run technical support for PHP, as well as a variety of official mailing lists to help you get answers when you need them.
  • PHP is advancing. With the release of PHP 5, PHP has introduced features that have long been waited for, including more comprehensive error handling, better object orientation, and, of course, more speed.
  • PHP is fun! As I am sure you will agree while using this book, PHP is an enjoyable language to use – very little time is spent debugging code, and there is a large selection of pre-written functions available.

    The HTML relationship
    When used to output HTML content, PHP is embedded inside HTML in code islands. The most common way to open and close PHP code blocks is by as shown in the script below: (don't worry about what the code means for now)