PHP Introduction and Includes
Example: None

Requirements: PHP enabled host

Previous Knowledge: No previous knowledge required

Introduction: If you feel that HTML is restricting your creativity or from expanding your website, then it's time to graduate to PHP. PHP makes coding a WHOLE lot easier and more fun.

There are many ways that PHP could be used for. However, the file extension where the PHP script is to be embedded HAS to be either in .php, .phtml, .php3, or .php4 for it to work. You would have already heard of .php from other websites, and of .phtml from neopets.

If you insert a PHP script into a file that reads index.html, it wouldn't work. It would work, however, in a file that reads index.php. BUT PHP could be embedded into HTML coding. It's important that you understand this before we go on. If you don't understand, read the paragraph again until you do.

The most popular use and the easiest to grasp is the PHP include. That is, an external file with a php extension or .txt is "included" inside another php page no matter where that external file may be. I'll explain this further later on, but for now think of php inclusions as frame coding, but minus the hassle.

However, understand now while it's still early that you CANNOT include a domain eg. You can only include files WITHIN your directory.

I know of four ways in which PHP could be written:

1.  <?php         ?>
2.  <?             ?>
3.  <%            %>
4.  <script language="php"> </script>

Now I tend to use #1, <?php ?> because this is the universal coding. It works on any website with PHP installed. In some cases the other three doesn't work on some servers. So for the sake of this tutorial I will be referring to <?php ?>. Once you're comfortable with PHP, you may start experimenting with the other three.


PHP Includes

1. The first thing that we're gonna do is to look at your website. Every website has a head, a body, and footer. Compare this to yours:

<!-- Head Starts Here -->


<body bgcolor="#000000" text="pink">

<div style="left:15; top: 100; position: absolute">

Links: <a href="#">1</a> 2 3 4
<!-- Head Ends Here -->

<!-- Body Starts Here -->


<center><img src="me.jpg"></center>


Hi! This is my website and this is my picture. Click the above links to navigate aroubd my awesome site.

<!-- Body Ends Here -->

<!-- Footer Starts Here -->



<!-- Footer Ends Here -->

Now understand the above example. The header starts it all of: where your CSS code is, the title, the whole shebang. The body is, naturally, where the content of your website is. The footer ties any loose ends: the divider code, the body and html, etc. Now what I would like you to do now is to copy the whole thing above and save it as test.php.

2. Now I would like you to cut out the head from test.php and paste it onto another notepad window. Save this as header.php. A lot of websites will teach you to end it in .txt, and that's perfectly fine. However, when it comes to advanced PHP inclusions, such as the dynamic inclusion, you would need to change it to php extensions, so save yourself the hassle and end it as .php now.

3. Do the same for the body and the footer. For the body, name it test.php (yes, overwrite the previous file). for the footer, name it as footer.php

4. Go back to your "test.php" window. Add the boldened lines below:

<?php include('/tutorial/header.html'); ?>


<center><img src="me.jpg"></center>


Hi! This is my website and this is my picture. Click the above links to navigate aroubd my awesome site.

<?php include('/tutorial/footer.html'); ?>

5. Do you understand what the above means? Whenever you need an external file embedded into a page all you have to do is write <?php include('/tutorial/index.html'); ?> somewhere where you want that file to appear in the existing page and that file would be embedded into the page. If the file is on another directory, you would need to use this:


The above file path works for most servers. However, sometimes it would read /home2/domain or something else, so you would have to check with your host.

Note that, because it requires the file path and not the website address, you can only include files WITHIN you directory and not files from other websites. =) This is a protection so people can't include YOUR files into theirs.

6. Test the page. It's working wonderfully, doesn't it? But now all you have to do is to edit header.php and footer.php instead of constantly having to change EVERY SINGLE page whenever you change layouts.

7. What about your other pages that uses the same layout as index.php? Well, firstly, prune the site of any coding that's already included in the header.php and footer.php so that you're left with the "body". Add the php-includes of the header and footer exactly as you've done in test.php. Save what's left of the page as .php. For example, you've finished pruning about.html. So you will save about.html as about.php.

8. Play around with it: change the bg colour in header.php and refresh test.php and about.php... it changed without you ever having to touch test.php or about.php. Do you understand what this means?


9. There you go! A day's work will save you the hours of work in the future when changing layouts. Now you know how to write one type of PHP! Well done! =)

Fragiled Arts 2005-2006 Victoria Galang. All Rights Reserved.