PHP tutorials: Include

Introducing words: I know it's not necessary to add one more of these tutorials in the Internet, but for some personal reason I feel I have to. I'm a design and graphic addicted person and therefor I find coding sometimes difficult. PHP is a popular kind of coding and sometimes it's hard to find a tutorial that is adapted for newbies.

This tutorial is an easy and short introduction into PHP and you don't need experience in PHP for this, but with HTML.

That's the plan:

01. First of all we need to create a new HTML-document (html, title, body, etc.), code an ordinary table that looks similar to the example and save it as a PHP-file.
Your code should now look like this:

HTML code (table only):

<table width="60%" border="0">
 <tr>
   <td colspan="2"><!--HEADER ROW--></td>
  </tr>
 <tr>
   <td width="60%">
      
<!--CONTENT-->
      Welcome to this tutorial about incl....
</td>
    <td width="40%">
      <!--NAVIGATION-->
      <a href="#">Link</a><br>
      <a href="#">Link</a></td>
 </tr>
 <tr>
   <td colspan="2"><!--FOOTER ROW--></td>
 </tr>
</table>

02. To get an included PHP-layout we need to seperate this code into three parts: header.php (red), index.php (yellow) and footer.php (blue). (The navigation is integrated in the footer.php here, but you could also create an own document for it, e.g. menue.php.)

03. After the code is seperated the different files will look like this:

header.php:

<html>
<head>
<title>PHP tutorial</title>
</head>

<body>

<table width="60%" border="0">
 <tr>
   <td colspan="2"><!--HEADER ROW--></td>
  </tr>
 <tr>
   <td width="60%">

index.php:

      <!--CONTENT-->
      Welcome to this tutorial about incl....

footer.php:

</td>
    <td width="40%">
      <!--NAVIGATION-->
      <a href="#">Link</a><br>
      <a href="#">Link</a></td>
 </tr>
 <tr>
   <td colspan="2"><!--FOOTER ROW--></td>
 </tr>
</table>

</body>
</html>

04. Let's leave it at that and turn to the include funtion. You only need the index.php for this, because everything else is build around this file - the core of each website.
include:

<?php include('header.php');?>

      <!--CONTENT-->
      Welcome to this tutorial about incl....

<?php include('footer.php');?>

To get a preview you need to upload these three files at your server and open the index.php. You'll see that the header and footer are now included in the index.php. This way, like step four, you need to code the rest of your subpages. If you want to link the navigation don't adress a target, but leave this out.

Back