In this article we'll introduce you to one of the Internet's hottest and fastest growing server side programming languages, PHP. PHP (or Personal HomePage Tools) was created by Rasmus Lerdorf over three years ago to track visitors to his homepage. PHP has since evolved into a powerful server-side markup language with syntax that resembles a mix between Perl and C.

Why Use PHP

Most Web developers know that Web pages are more than pretty pictures and text; these days, even the most amateur of sites strive to have some sort of animation or interactivity. Most of the higher end sites have features such as discussion forums, search engines and/or shopping carts. PHP enables you to add features quickly.
The old fashioned approach to adding interactivity was to use CGI scripts in Perl. The problem is that CGI is not very scalable; each new request to a CGI script requires the server to start a new process in the kernel, which uses both CPU time and memory, making CGI scripts much slower which in turn make multiple concurrent CGI scripts run very slowly. PHP solves this problem by becoming a part of the Web server itself, saving the end user a considerable amount of load time.
Another reason to use PHP is because it's free. That's right, PHP is an open source project, meaning that any user can download both the source code and executables for PHP and install them on their computer for free. Because PHP is open source, it is solid and is constantly being improved by many experienced programmers. It is currently available for all major platforms.
Finally, PHP is easy. If you know C or Perl, learning PHP is a cinch. The language is a mix between the two, taking the best features from both. Plus PHP adds features to solve common problems that programmers often encounter when programming for the Web. For example, you can embed PHP code directly into an HTML file, whereas in Perl and C you would have to use additional print statements to output HTML. Another advantage to PHP is its native database support for 12 databases (including mSQL and mySQL), which allows access to the databases directly through SQL statements. And if you don't know either of these programming languages, then PHP is an excellent gateway into the world of programming.

What is PHP?

So now that I've whetted your appetite, let's learn some more about PHP. To start off I'll throw some simple PHP code at you (this assumes you understand HTML):
basic.php3

1: <html>
2: <head>
3: <title>A Basic PHP page</title>
4: </head>
5: <body bgcolor="#ffffff" text="#000000">
6: <?php
7:  $date = date( "l F d, Y" );
8:  print( $date );
9:  ?>
10: </body>
11: </html>
The function of this code is simple. First we start off the document with the normal HTML headers, nothing new there. What is new is the material on lines 6 through 9; here we see simple PHP code which will print out the current date. Here is a line by line explanation of what happens in lines 6 through 9:
Line 6: Tells the PHP preprocessor that the PHP code has begun. This is like the <html> at the beginning of the document which tells the Web browser to treat the document as an HTML language.
Line 7: A lot going on in this line - first we initialize (assign a value to) the variable $date. The value is a formatted date generated by PHP's built in date function.
Line 8: Here we use the built in print function to print the variable $date out on the screen.
Line 9: Tell the preprocessor to stop interpreting PHP code.
This is a basic example of PHP's features, and a little bit of source code to give you the idea. This article's purpose is to introduce PHP, rather than write a tutorial on PHP. Subsequently, I'm not covering the syntax in depth but rather giving a taste of the language. Other tasks that PHP is especially good at are database access, disk access, networking and text manipulation. You can find articles that cover these features on the sites listed below.

Conclusion

In this article, we have discussed the motives for using PHP, what PHP is and even gotten into writing some source code. We have also gone over some of the history of PHP. However, this is meant only as an introduction, to help you on your way to becoming an experienced PHP programmer. For more information, I have provided a list of links which will serve to help further your knowledge of PHP. Happy trails!

Links

Author Info

Stirling Hughes can be reached at stirling@scalarsplit.com, or at www.scalarsplit.com.