CSC 105

Lab: Introduction to PHP

 

The purpose of this lab is to give you practice at some simple applications of PHP. As you know, PHP is a server-side scripting language. Documents containing PHP scripts should end in .php to distinguish them from normal .html files. To work properly, they must be viewed from a web server with a PHP interpreter installed and running. Thus, rather than testing .php files on your local machine, you should upload them to Beowulf for viewing.

Included Files

One convenience of PHP is that you can include one file within another, as seen in this example. Thus, if you wish to have repeated content in multiple pages without using frames, you can write a single file holding the static portions of the page, that gets included with the different content in each page.

Let's practice this by putting an advertisement on this page. Save the source with a .php extension, and save the fragment below in another file called advert.php.

<div style="position: absolute; width: 150px; top: 10px; right: 10px; font-style: italic;
z-index: 255; background-color: navy; color: yellow; padding: 10px;">
This page was brought to you by Huge Conglomerate, Inc.
</div>

Now you should be able to make the advertisement appear in this page by including the following line inside this page:

<?php include("advert.php") ?>

Any page that needs the advertisement can get it by including the line above. You can do similar things with a header, footer, and navigation bar.