FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 01, 2002
What You Should Know About PHP

Jason Gilmore

WebReview.com: What You Should Know About PHP

Rank: 3

Popular PHP Features

The PHP scripting language is a handy addition to any web developer's toolbox. Here are three of its more popular functions:

Database to web site integration. PHP can mine data from your DB and convert it to HTML.

File parsing. PHP is handy for pattern matching and replacement, string-splitting, and more.

Math calculation. PHP has predefined math functions to handle just about any need, including generating random numbers.

"Gilmore, I want the copyright statement on each page of the company web site modified by 3pm!" I recoiled into my chair, already lost in thought and despair as my project manager continued to scream a list of various other site modifications into my ear. "But boss, our site is over 300 pages!" I lashed back, the tears already welling up in my eyes. "Well then, I suggest you start typing!" he answered, an evil grin creeping across his face....

This was one of the main themes of a recurring nightmare I had back in the days when site developers were generally stuck hard-coding data in order to build the content and breadth of their web sites. Fortunately, the nightmare quickly vanished when I found one of the web developer's best kept secrets, PHP.

A little PHP history

PHP, defined by its developers as a "server-side HTML-embedded scripting language," provides both novice and expert programmers alike with an extremely powerful set of tools which they can use to serve dynamic content to their web site. Created in 1994 by Rasmus Lerdorf as a personal project, a core group of developers soon joined Rasmus and continued to develop the software package that, as of November 1999, has been installed on over one million servers worldwide according to Netcraft. Furthermore, it is the most popular Apache server module, edging out industry behemoths such as Microsoft FrontPage and mod_perl.

In short, although you never seem to hear the end of praise for other popular dynamic content solutions (such as Active Server Pages), PHP has undoubtedly become a worthy alternative. Its viability is further proven by the fact that such giants as Mitsubishi, Redhat, and Distributed.net have chosen PHP as their solution for serving dynamic content. This is just to name a few—there are many other prominent sites out there with PHP-based pages.

Leon Atkinson, in a previous Web Review article (Uncovering One Of The Web's Best Kept Secrets), took the time to provide more details regarding the history and future of PHP.

What can PHP do for you?

But enough small talk. What can PHP do for you? How can it keep your boss from tearing your head off in a web-induced rage? Why will PHP make you look and feel like a web demigod? Let's begin by summarizing PHP's general functionality. Afterwards, we will cover a few examples that you will be able to immediately incorporate into your own web site.

The PHP language can be thought of as a hybrid between C, Perl and JavaScript, incorporating many of the most prominent features of each into one complete language. Therefore, a user with experience in any of these languages will quickly catch on to the basic syntax premises surrounding PHP. Let's review some of the more popular features of the language:

  • Web / Database Integration—One of the most coveted features of PHP is the ease with which it can be used to mine information from a database, and in turn convert that data into HTML format for ease of reading. Support is included for many of the most popular database servers, including MySQL, Oracle, the Microsoft SQL server, and PostgreSQL.
  • File Parsing—While Perl has been long hailed as the king of regular expressions, PHP is certainly no slouch. PHP pattern syntax is remarkably similar to that of Perl, allowing for such useful functions as global pattern matching, pattern replacements, and string-splitting. A complete library of functions allowing one to traverse and convert strings to various formats is also at hand.
  • Mathematical Calculation—PHP can perform mathematical calculations with great precision, and has predefined functions to handle most every need. Functions to generate random numbers, convert between various bases, calculate geometrical values, and calculate square roots are readily available, just to name a few.

I would like to stress that the features described above are only a minor part of what PHP has to offer. The following table summarizes a number of supported features.

Table 1

Summary of Popular PHP Features
  • Mathematical expressions   • Regular expressions
  • Date and time   • Network
  • SNMP   • Apache-related
  • Filesystem and directory   • FTP
  • LDAP   • PDF (Adobe)
  • Encryption   • Array

Installation Note

Please note that our sample files including PHP code are denoted with the '.php3' extension. This is due to an addition to the Apache httpd.conf file that requires a listing of PHP-related MIME types (there are two of them). You can rename these files if you wish. If you have not already installed and configured the software, you can learn more about it in The PHP documentation.

Getting started with PHP

Now that you have a basic idea of what PHP is capable of, let's turn toward gaining some practical knowledge on the subject.

As stated earlier, PHP is HTML-embedded. Basically, this means that, like JavaScript, we can simply "escape" to PHP code, and then back to HTML as needed. An example of how this is accomplished (along with another useful little trick) is shown in the following file, which we will call first_script.php3.

Example 1: A basic example

<HTML>
<HEAD>
<?
// a comment can be inserted like this
// designate some variables
$bgcolor = "green";
$textcolor = "white";
?>
<TITLE>First PHP example</TITLE>
</HEAD>
<BODY <? Print "bgcolor='$bgcolor' text='$textcolor'"; ?>>

<H3>A PHP-enabled page!!!</H3>

</BODY>
</HTML>

View first_script.php3 as it would be seen in the browser. Pretty cool, huh? It's important to notice how the PHP and HTML code are intertwined, and even more importantly how PHP outputs code (such as 'bgcolor') that is ultimately interpreted to be HTML. However, this is only a very simple example of what PHP can do. As you may have surmised in the above example, although variables are being used, it is still rather static in nature. What if we wanted to be able to make timely, simultaneous changes to many pages of a large web site? With PHP, it's not a problem!

Remember my boss's "nightmarish" request to modify the copyright statement on all web site pages? As a final example, let's see how PHP could be used to perform this task quickly and efficiently:

Example 2: The INCLUDE statement

A function entitled 'INCLUDE' allows you to insert a certain piece of code or text into another PHP file simply by calling the function 'INCLUDE' along with the name of the file to include. Therefore, if we wanted to insert a copyright statement at the bottom of each page within our web site, we would start by creating a file, say 'copyright.inc' (.inc extension stands for include).

File: copyright.inc

<DIV ALIGN=center>
Web Review copyright © 1995-2000 Songline Studios, Inc.<BR>
Web Techniques and Web Design and Development
copyright © 1995-2000 Miller Freeman, Inc.<BR> All Rights Reserved </DIV>
You could insert this file into any PHP-enabled file with one simple command:

<?
INCLUDE "copyright.inc";
?>

Therefore, you could now greet the boss's seemingly impossible demands with a, "No problem." Simply make one modification to the 'copyright.inc' file, and Voilà! The entire site is changed!

Additional PHP Resources:

www.php.net
www.phpbuilder.com
Geocrawler PHP Mailing List Archives

Final thoughts

Now that you've had a brief introduction to PHP, you'll be prepared for our next article when we delve much deeper into the language and the various template methodologies available to automate web sites. Ultimately, the use of templates will allow you to spend more time improving the site and less administering it.


Web Review's Class of 2000
What You Should Know About PHP

RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:
    MEDIA CENTER  more
    NetSeminar
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
                                   
    INFO-LINK

    Resource Links: