2009-09-01
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 1 of 5 )
Ready to learn PHP? This is the first in a series of articles by Jeff Cogswell, well-known Dummies author and teacher.
PHP is a programming language that was built from the ground up with the Internet in mind. At its most basic level, PHP lets you write code that runs on the web server, code that generates an HTML page that can be viewed in a web browser. But PHP goes far beyond this simple level; you can use it to write entire Web server code for managing and controlling web applications. In this article I'll get your started with the basics of PHP. (I'm assuming you have a basic understanding of programming in general, such as what a variable is and what it means to call a function. I'm also assuming you know at least a small amount of HTML.)
So let’s start with the basics. First, you’ll want to make sure you have some software installed. I don’t have room here in this article to explain how to install and configure these packages, but the web sites where you get them have plenty of resources. Here are the two things you’ll want:
- A web server, such as Apache, which you can get here: http://httpd.apache.org/
- The PHP processor installed on the web server: http://www.php.net/downloads.php
Now let’s begin. Consider a simple HTML page like this:
<html> <body> <h1>Hello world</h1> </body> </html>
If you save this to a file with an .html filename extension, put it on a web server, and open it up in a browser, you’ll see something like Figure 1.

Figure1. A simple HTML file with an .html filename extension
The file is saved on the web server, and the web server software simply sends the file down to the browser as-is. The browser receives the file, reads the HTML, and displays the text on the screen, formatted according to the HTML. (In this case, the words Hello world are shown in a header font and size.)
But instead of just sending a file as-is down to the browser, why not have the web server first do some processing of the file? This is where PHP comes in. Look closely at the following code, which looks much like HTML, and the results in the browser. Here’s the code:
<html>
<body>
<h1>Hello
<?php echo("world"); ?>
</h1>
</body>
</html>
Save this into a file called hello.php (make sure it has a .php extension!) and open it in the browser. You’ll see the same thing as you saw with the previous file, as shown earlier in Figure 1.
But this time, instead of just sending the code down to the browser, the web server starts up the PHP processor in response to the .php filename. The PHP processor reads in the entire file, and runs the code that’s inside the <?php ?> tags. In this case, the code the runs is this:
echo("world");
(which is enclosed in th <?php ?> tags.) This code simply writes out the word “world”. Here's the order in more detail: The PHP processor reads the .php file, and generates on-the-fly a new HTML file, which is in turn sent down to the browser. This new file is a simple HTML file, with the <?php ?> tags replaced by the output from their respective PHP code.
The PHP code echo("world"); simply prints out the word “world”, and that’s what gets put in the resulting file, replacing the PHP code. So in other words, this line:
<?php echo("world"); ?>
will be replaced with:
world
(The keyword echo simply writes to the resulting HTML file. But note that an actual file isn’t written to the web server’s hard drive; the Web server just takes the results and sends them on down to the browser.)
The PHP code is pretty simple; the echo keyword is followed by the text you want to print out, which is enclosed in quotes, which is in turn enclosed in parentheses. Also notice that statements in PHP end with a semicolon (much like C++ and Java if you're familiar with either of those languages).
If you open the .php file in the browser and click View Source, you’ll see exactly what I mean. The browser didn’t receive the original .php file. Instead, the browser received the output from the PHP processor, which is just HTML and looks like this:
<html> <body> <h1>Hello world</h1> </body> </html>
Compare this to the original .php file, and you’ll see where the replacement took place.
![]() |
|


