Are you being served? An introduction to PHP

Besides being an easy language to learn, PHP offers a great deal of flexibility and server-side punch for your Web sites. PHP supports ODBC and XML as well as Java servlets.

When I started writing this article, I promised myself I wouldn't editorialize or evangelize the usage of PHP. Instead, I'd just write a nice little article about this cool server-side scripting language that's currently used on over 1.4 million web servers.

I wasn't going to go on about the blazingly fast parsing engine in PHP4, or its database connectivity functions. I wasn't going to discuss the XML support, Java servlet support, native session handling and IMAP functions. Most of all, I wasn't planning to enter into the "open source vs. commercial software" debate, and tell you about the hard-working group of developers working day and night to add functionality and upgrades, based purely on user responses and requests.

But it would be just silly of me not to mention all of that. Since version 3 (we're now to the first official release of version 4.0.0), PHP has proven to be a viable and stable solution for everything from personal to corporate to E-commerce web sites, and not just for me, but for thousands of developers worldwide

Server-side scripting languages aren't new; you have ASP, you have Cold Fusion, and for some people, that's the solution they want. People have to make their own choices, and I'm not going to say that one language is better than the other—only you can decide if PHP should be your language of choice. I can only tell you from my experiences with all the popular solutions—Active Server Pages, ColdFusion, Java Server Pages, Perl and PHP, on numerous platforms and various Web servers—that PHP has been the right choice for our clients since I stumbled over it about a year ago. It's flexible, fast, simplistic in its requirements, yet powerful in its output.

PHP can do anything you want, except sit on its head and spin. With a little on-the-fly image manipulation and DHTML, it can probably do that, too. If you're coming over to the PHP world from another language, I promise that you can pick it up in no time: the syntax borrows from Perl, C and Java, so if you have even the most basic understanding of programming languages, your first PHP project will be a breeze.

Part of the simplicity of learning PHP is in the function names. That may sound odd, but it makes it easier to learn a language when the language makes sense! Suppose you're working hard on something and stop and think "Hey, I need to trim some whitespace from this string, I wonder what function does that?" and you head over to the PHP Manual and look up "trim" and, amazingly enough, find that the function is called trim(). Or, that to set a cookie you use the setcookie() function.

Additionally, the developer community is extremely helpful; if you can't figure out how to do something, post a question on a PHP mailing list and within minutes, you'll have an answer. Because all of the mailing lists have been archived for years, a quick search will likely yield an answer as well. You can't ask for a better support system!

System requirements
The first concern for new PHP scripters is usually the OS/Web server/database question: What do you need in order to run PHP? Well, what do you have? A Windows box? Not a problem—you can get up and running with PHP4, MySQL and Apache in half an hour or less. Swap out Apache with IIS or PWS, use Access or MS-SQL Server instead of MySQL—you can do that, too. Name a flavor of Linux or Unix—PHP compiles in heartbeat. Apache would be the server of choice, but PHP's open API makes it available to other Web servers as well.

 Basically, come up with any combination of OS/Web server/database and if you can't get PHP to play nice out of the box, just send a message to the PHP mailing lists and either someone will have a solution, or it'll get added in to a subsequent build. That's the beauty of open source.

Making a switch from one programming language to another is a big decision—chances are good that you've shelled out a lot of money to meet hardware and software requirements in order to develop in ASP or ColdFusion. Those requirements don't exist with PHP—it's free, cross-platform, and as stable and fast as the machine you put it on. In my arsenal, I have PHP running on the following: a Red Hat box, two SuSE boxes, two Windows 98 boxes, an old IBM Thinkpad running Slackware, and soon a Powerbook 1400cs running Yellow Dog. Processor speed ranges from 90 to 450 Mhz, and RAM amounts range from 40MB to 128MB. I have not had one problem in installation, configuration or consistency among the lot of them.

Where the language goes from here, no one knows. We're about a week into the first release of PHP4.0.0. Plug-in modules for code optimization and debugging are soon to follow, led by Zend Technologies. New developers are jumping on the bandwagon each day. The more new developers, the more new ideas, and the language will go forth and multiply. Learn it, contribute to it, all the world's better for it.

I'll show you a few snippets of the language, to introduce you to the syntax. I'll take the first example from the Scripting with ASP article on ZDNet, and rewrite it for PHP.

Sample codes: A
The first example prints the server time to the screen. PHP code lives inside start and end tags, juts like ASP. Wherein ASP uses <% and %>, PHP uses <? and ?> or <?php and ?> or <script language="php"></script>. Actually, there's a configuration option in PHP that allows you to use the <% and %> ASP-style tags as well.

Anyway, start the script with a PHP block that gets the current date, formatted however you want:
      $date = date("m/d/Y H:i:s");
Then add in some HTML:

Now print the date:
<p>The time is now: <? echo "$date"; ?></p>

and close your HTML tags:

Call it time.php, stick it on your PHP-enabled Web server, load it in your browser and you'll see something like:
The time is now: 05/24/2000 08:04:11

Using formatting parameters with the date() function, you can print the name of the day or month in words, abbreviations, whatever you want, and you can call the time AM or PM. The point is, you created a variable to hold a timestamp, and you printed it in the body of the HTML. Easy as pie.

Sample codes: B
Want to send mail? The mail() function takes four parameters:
  • recipient address
  • subject
  • message
  • any other headers

So, if you want to send mail to me, it would be something like:
$to = "";
$subject = "ZDNet Developer Article";
$msg = "PHP sounds so cool!  I can't wait to start.";
$headers = "From:";
mail("$to", "$subject", "$msg", "$headers");

You could build a message string using whatever text formatting you'd like. Your additional mail headers can be anything compliant, such as CC or Reply-To fields.

The mail() function uses a mail server specified in the PHP configuration file (php.ini). On Windows, you can set that server to a name of a machine such as or even localhost if you want to use something on your machine. On Unix systems, you set the path to sendmail, or qmail, or whatever you use. It's a snap.

Those are just two very basic techniques: echoing content to the screen and sending mail, but they're two of the more common tasks performed using a server-side scripting language. Subsequent articles will focus on these and other elements, such as database connectivity, regular expressions, image creation, using sessions, user authentication... Stay tuned!

Editor's Picks