DRH Internet Inc.
Website hosting technical support library

By: John Hanney
Contributing Writer
http://k2g.drh.net

Introduction To PHP

PHP is a general-purpose scripting language that can easily be embedded into HTML and enliven your Web pages.  PHP can give pages a more interactive or customized flare than it's possible with straight HTML coding.  In the following paragraphs, you will only be tackling the server-side scripting portion of PHP.  That's the code that communicates to the server and helps produce those wonderful dynamic Web pages you'll soon be creating.

To obtain the history on PHP or a more in-depth introduction about its versatility, there are two popular places you want to visit after reading this page.  They are:


The Preparation

Before you dive right in, you'll need at least a very basic understanding of HTML and how to upload Web pages onto your site.  This would include transferring files using FTP and a familiarity with Telnet or SSH to rename files or set file permissions.  Once you know you have a grasp on handling HTML pages, adding PHP to the picture should be easy and exciting.


A PHP Tutorial - Short & Simple

I'm ready, how do I get started?

Let's begin by introducing the tags that denote a block of PHP code.  You can do that in two ways:

  •  <?php . . . ?> 

         or

  •  <? . . . ?> 

You can write code with or without the letters "p-h-p".  It's best to bring that up now because later you may see examples that use one or both of these methods.  Where you see the 3 dots is where your PHP code will go.  If you have experience with other programming languages, you may notice that PHP code has similarities to C programming and Perl scripting. 

The first function we'll try is echo.  What does echo do?  You guessed it.  It displays whatever we tell it to the screen.  Here's how we display the word hello to the screen:

  •  <? echo "hello"; ?> 

By placing the word hello between double-quotes, we have created something in the programming world known as a string.  We could assign the word hello to a variable and then display the variable.  The code below would display the word hello to the screen just like the code above. 

 <? 
 $myword = "hello"; 
 echo $myword; 
 ?> 


The only difference between this code and the code above it is that we assigned the hello string to a variable first, and then we displayed the variable.  We can go one step further and display using a combination of strings and variables together.  Here's some code that demonstrates string text with a variable.

 <? 
 $skycolor = "Blue"; 
 echo "The sky is $skycolor."; 
 ?> 


     or

 <? 
 $skycolor = "Blue"; 
 echo "The sky is ", $skycolor, "."; 
 ?> 


     displays

 The sky is Blue. 

Up to this point, you now have enough of the basics to apply some code to an HTML page.  In the next section we'll demonstrate how you can put this new knowledge into practice.

Of course this is a very brief tutorial, so there's much more to PHP than displaying strings and variables.  Most tutorials start out in a similar fashion by presenting examples using echo as we have done here.  After that, the tutorials vary in style and direction.  Be sure to see some of the different tutorials that are available to you out on the Web when you've read through the one here.


Adding PHP Code To A Web Page

A great way to get a feel on how PHP works is to see it in action.  Let's create a Web page that displays the IP address of the current visitor. 

First, create a simple HTML Web page using a text editor you are most familiar with.  On the Windows/PC platform this may be Notepad or Wordpad (from your Accessories folder), or on the Unix/Linux platform this may be vi (visual editor) or pico.  Start the editor.  Then copy and paste, or key in, the text from figure 1 below. 

<html>
<head>
<title>Display Current IP Address</title>
</head>
<body>
<h2 align=center>Display IP Address</h2>
<p align=center>The current IP address is:
<b>000.000.000.000</b></p>
</body>
</html>

Figure 1
[Download Source]

Now if we were to save this code just as it is and display it from the browser it would look something like this:


Display IP Address

The current IP address is: 000.000.000.000


Figure 2

You've got to admit, that page in figure 2 would be a pretty boring if we were to leave it that way.  Now, let's replace that string of zeros with some PHP code to make those numbers reflect the visitor's IP.  We will introduce a system variable to help accomplish this.  The new code is highlighted in yellow.

<html>
<head>
<title>Display Current IP Address</title>
</head>
<body>
<h2 align=center>Display IP Address</h2>
<p align=center>The current IP address is:
<b><? echo $HTTP_ENV_VARS['REMOTE_ADDR'];
?>
</b></p>
</body>
</html>

Figure 3
[Download Source]

The next step is to save this page.  But instead of saving it as an ".htm" or ".html", give it an extension of ".php".  (Maybe you can call it "my_first_page.php".)  Upload the page to your website insuring that the file is set with the same permissions as any HTML page would normally have.  Then display it with your browser:

  • http://www.yourdomain.com/my_first_page.php

That's all there is to it.  How's it look?  It's probably not the most exciting piece of work you've done, but enough to inspire many more.  If you'd like to walk through another example that's a bit more advanced and contains some code that's somewhat unique to DRH Internet, see our page entitled "Adding PHP Code To A Web Page: Supplement"


Additional Online PHP Developer Information

There's quite an abundance of PHP information just a click away.  Many sites dedicated to PHP have great sections for the beginner.  For another good introduction to PHP and it's basics, visit CNet's "Builder.com" website:

Zend Technologies has something for the beginner and more.  Webmonkey reveals some information for beginners through Tim Ziegler's article:

Once you've got a handle on the basics, you'll need to look up and try some of those advanced functions.  An online PHP reference is just the place to go.  "PHP.net" is one of the homes of PHP:

Do you have a thirst for more?  Then visit the place for the serious PHP coder: 

Is there anything we missed?  Sure.  We just can't include them all here.  But Yahoo has done a good job.  Check this out on an occasional basis for the latest links on the topic: