|
PHP Includes
Hypertext Preprocessor (PHP) is a form of
server sided HTML-embedded scripting that is increasingly growing in popularity. It can do
what most CGI programs can do, such as generate dynamic pages or set cookies. One reason
why it's so popular though is its support with a wide range of databases, allowing the
programmer to manipulate the script to do what he or she wants. PHP is extremely simple to
learn if you know some basic concepts of programming, such as if/then/else, looping and
using variables.
However, to use PHP to organize content on webpages, all you need to know
is the "include" function. Like in SSI, you are able to link external HTML files
into your PHP page. Before you begin this short tutorial, please remember to do two
things: check that your server supports PHP, and make sure you upload your pages to your
server before testing them out or they will not work as PHP is server-side.
First of all, save your "template" page as a PHP document
(change the extention from .htm or .html to .php). For this tutorial, we shall call this
PHP page main.php
Open the source code for main.php, and where you want the content to show
up put in the following code:
<?php
if (file_exists("$id.htm"))
{include ("$id.htm");}
else
{include ("error.htm");}
?>
Make sure that you also have a HTML containing the content. For this
tutorial, we will call it testing.htm. Upload main.php and testing.htm to your server, and
locate your file by typing in http://www.yoursite.com/yourfolder/main.php?id=testing. Now
here's the hard part to understand.
Think of the term "id" as a variable, like the variable x used
in algebra. In the URL, you have just assigned the value "testing" to the
variable "id". In the PHP code which you have copied and pasted in to your
document, where ever $id occurs (in PHP, a $ denotes a variable), the value
"testing" will be replaced instead. So literally, the PHP code is 'including'
testing.htm into your PHP page.
Also notice that there is the use of a "if else" statement in
the PHP code. What it's saying is that if "$id.htm" exists, then include
$id.htm. Otherwise (else), include the error file which is error.htm. In order for
that to work, you must have also prepared in advance "error.htm" as your 404
error page.
I hope this has been helpful to you! Please use the contact
us form to forward all feedback, comments or questions you might have with PHP
includes.
|