Change background image
  1. What's up? I see you're viewing as a Guest. How about registering, it only takes like 2 minutes. This will enable you to do more on our forum and stay updated.

[TUT][PHP+HTML] Simplifying code via Includes

Discussion in 'PC General' started by JesusLover666, Jul 6, 2011.

Thread Status:
This thread is more than 180 days old.
  1. JesusLover666

    JesusLover666 Junior Member Member

    So I've worked with PHP for years now... This tutorial was written by me, and posted on one other forum. This will be part one in a series of tutorials I'm going to write - from the basics (like this one) to more advanced stuff (AJAX, etc) to databases.

    What I'm going to cover in this tutorial is how to use PHP with your HTML files, for the main purpose of simplifying coding and easier changes. For this tutorial, I'm going to assume you have a webhost - whether it be on your PC (Xampp, WAMP, etc) or a server/shared host that you're paying for/got for free.

    Most hosts include PHP built in. If yours doesn't, you may want a host that's with the times. xD

    Ch. 0 - USING PHP

    In order to get your server to parse PHP, we're going to need your file to be renamed to .php (NOTE: This is not always mandatory, and you can have PHP parse any file type, but I'll get to that in another tutorial!) after you have your index.php/whatever.php, you need to add your PHP tags.

    will say "PHP, start parsing on this line"
    will say "PHP, you're done now! Go back to using the HTML parser!"

    Another small but important note, and something even I mess up on occasion is that with PHP code, every line (except continued statements - we'll get to that!) must end with a semicolon ( ; ).


    The first thing you want is an HTML file. I'll be providing code snippets all the way through.

    So let's start with simplification of code. Let's say you have a navigation bar that you want on every page. Sure, you could copy and paste it to every html file, but what happens when you add a new page? You have to update all your links. This is where includes come in!

    Here's our HTML nav bar:

    <a href="index.html">Home</a>
    <a href="aboutus.html">About Us</a>
    <a href="portfolio.html">Portfolio</a>
    <a href="contact.html">Contact Us</a>
    (Very simple, but it'll do the trick!)

    Open the page and ensure that you see the navigation - view the source, too (Press Control U!) and take note of it.

    Now, let's make a new folder in our site's folder. Call it "inc"

    In this folder, create a new text file. Rename it to ""

    In, put the whole nav bar code.

    Now, delete the navbar HTML from your index.php/whatever.php file. In it's place put
    <?php include("inc/"); ?>
    Now re-open your PHP page, and hit Control U (aka get to view source).

    Does it look similar to the orginal? It should! PHP has included your HTML code into the site. This can be very useful if your code would be hundreds of lines, but your using parts of the same code everywhere. I use it for navigation bars, sidebars, sometimes ads, and footers, mainly.

    This can also be used to make one change (In your file) and have it reflected across every page including

    I hope this helped, and expect some more tutorials soon! (I'm writing these as I go, lol)
    1 person likes this.
  2. dns

    dns Active Member Admin

    Bro thank you for this and i'm seriously looking forward to more tutorials here on PHP from someone who personally knows it. I can manage to see what's already written and edit it slightly to my advantage, but as far as writing something from the ground up goes, i've never actually done it. I'll be anxiously waiting for your PHP stuff, and thanks for this.
Thread Status:
This thread is more than 180 days old.

Share This Page