• home
  • forum
  • my
  • kt
  • download
  • Using PHP include() – Make things easy for yourself!

    Author: 2009-03-09 10:27:01 From:

    Using PHP include() – Make things easy for yourself!

    The PHP include() statement can really help when it comes to editing site layout

    Imagine a static site with 40 pages all with the navigation menu hard-coded into all 40 pages. What happens when more links need to be added? ANSWER: Open all 40 files and change the links manually! Not if you use the PHP include statement!

    Basic Static PHP file:

    <ul>
    	<li id="nav1">Home</li>
    	<li id="nav2">Page1</li>
    	<li id="nav3">Page2</li>
    	<li id="nav4">Page3</li>
    	<li id="nav5">Contact</li>
    </ul>
    <h1>Welcome to my site</h1>
    Fig 1.1 – Navigation Hard-Coded into PHP file

    If you used the same layout structure as Fig 1.1 for every page in your site then any change to the navigation bar will need to be changed across EVERY page

    The PHP include statement allows you to put the navigation menu into a separate file such as Fig 1.2 below:

    <ul>
    	<li id="nav1">Home</li>
    	<li id="nav2">Page1</li>
    	<li id="nav3">Page2</li>
    	<li id="nav4">Page3</li>
    	<li id="nav5">Contact</li>
    </ul>
    Fig 1.2 – navigation.php

    You then ‘include’ the navigation.php in any page where you want the navigation menu to appear by using this very small piece of PHP code:

    
    Fig 1.3 – The PHP include statement

    PHP include statement implementation:

    
    
    <h1>Welcome to my site</h1>
    Fig 1.4 – How to implement the PHP include()

    You could put a lot more than just the navigation menu into a separate ‘include’ file.

    PHP include allows you to split your site into much smaller and manageable chunks. It is also a form of ‘future-proofing’ your site, meaning that it would be much easier to change your site design when necessary.

    Look at the possibilities:

    PHP Include () - Possibilities

    PHP Include () - Possibilities

    A ‘real-life’ example from of the sites in my portfolio where I have used the PHP include statement a lot:

    
    
    
    
    <img src="images/home2.jpg" alt="" />
    
    Welcome to ***** 
    
    
    
    Real Life example of the include statement in action!

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (41)
      Cookies and Sessions (12)
      Counters (15)
      Database Related (34)
      Date and Time (15)
      Development (22)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (14)
      Error Handling (8)
      File Manipulation (36)
      Flash and PHP (6)
      Form Processing (22)
      Guestbooks (12)
      Image Manipulation (26)
      Installing PHP (7)
      Introduction to PHP (29)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot