+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11
Like Tree1Likes

Thread: TUTORIAL: Save time using the PHP include Function <?php include

  1. #1
    Dan
    Dan is offline Programmer
    Join Date
    Jun 2006
    Posts
    145
    Rep Power
    0

    TUTORIAL: Save time using the PHP include Function <?php include

    One of the main problems faced by webmasters is saving time when it comes to creating websites. When starting a website, one may choose to create a navigation menu like the following to link to each page:

    Code:
    <a href="http://www.yoursite.com/index.htm">Home</a> - 
    <a href="http://www.yoursite.com/about.htm">About Us</a> - 
    <a href="http://www.yoursite.com/links.htm">Links</a> - 
    <a href="http://www.yoursite.com/contact.htm">Contact Us</a>
    Which looks like: HomeAbout UsLinksContact Us

    So now, you have your 4 pages - and all is well. But, as the site expands you need to integrate a forum - which leaves you with 4 pages to update. No problem at all... But supposing the site had 100+ pages! That would be a very monotonous time consuming job.

    This is where we bring in the php include command to really speed things up!

    Create just the navigation menu on its own, and rename all of your pages to .php

    In this case, the navigation menu will look like this:

    Code:
    <a href="http://www.yoursite.com/index.php">Home</a> - 
    <a href="http://www.yoursite.com/about.php">About Us</a> - 
    <a href="http://www.yoursite.com/links.php">Links</a> - 
    <a href="http://www.yoursite.com/contact.php">Contact Us</a>
    Which looks like: HomeAbout UsLinksContact Us

    Save this file as "menu.inc" as we will be including it elsewhere - hence the .inc extension.

    Now, open up your .php pages that the menu links to, and simply add the following code at the very top, to include the navigational menu on the pages:

    Code:
    <?php include("menu.inc"); ?>
    The code is telling the php page, to "include" the menu.inc file - and it does!

    Once this has been added, any changes in the menu.inc file are reflected across the whole site making updating a lot easier!

    Why stop at menus? This can be utilised for adverts, footers, headers, templates and much much more!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30
    I includes like this to do most of my PHP work. I don't see a point typing the same info over again and if you want to change it you would have to change it on each page. With include you change once.

  4. #3
    Dan
    Dan is offline Programmer
    Join Date
    Jun 2006
    Posts
    145
    Rep Power
    0
    Quote Originally Posted by Lop
    I includes like this to do most of my PHP work. I don't see a point typing the same info over again and if you want to change it you would have to change it on each page. With include you change once.
    Exactly the reason why I started using PHP! Of course once I discovered the powerful stuff it could do I never look back, I now build all websites in PHP.

    Hope some strictly HTML users find this tutorial useful!

  5. #4
    DevilsCharm's Avatar
    DevilsCharm is offline Programming God
    Join Date
    Jul 2006
    Posts
    884
    Rep Power
    0
    This is some PHP that I do know, I use the php include function on all of my pages. I have a global header and global footer, so I have to use it at least twice on all pages (but on some pages I have a third one).

  6. #5
    Kaabi Guest
    PHP has a lot of great functions, but I haven't heard of a PHP include one before. But then again, I know very little about PHP.

  7. #6
    xtraze is offline Programming God
    Join Date
    Dec 2006
    Location
    Sri lanka
    Posts
    911
    Rep Power
    0
    I find this really helpful, does this obey the CSS or just show as default ?
    can I put it inside a <div> ?

  8. #7
    ervan is offline Newbie
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Re: TUTORIAL: Save time using the PHP include Function <?php include

    all php working?

  9. #8
    Jerryn is offline Newbie
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Re: TUTORIAL: Save time using the PHP include Function <?php include

    I have recently started learning PHP again (stopped due to busy schedule) and from what I've understood, using .inc extension might not be the wisest thing to do. Using it for simple HTML files is ok, but if beginners forget that .inc's source code can be read by anyone, then they might include files that store very valuable PHP information. (password for database for instance)

    So my suggestion to beginners is to create a new folder in your main path called "includes" and keep every file you include there. then just link it properly. (e.g. <?php include("includes/config.php"); ?>)

    But since you're talking about creating the website layout, then you can also use .html extension. e.g. header.html, footer.html, menu.html etc.
    Alexander likes this.

  10. #9
    Join Date
    Jun 2010
    Location
    Vancouver, Eh.
    Posts
    4,020
    Blog Entries
    7
    Rep Power
    39

    Re: TUTORIAL: Save time using the PHP include Function <?php include

    Yes, it is often standard practise to add the .inc filetype to be forced through the PHP interpreter, however people often forget to do this or do not realize to do this after moving the script.
    Be sure to read the updated FAQ || Health is achieved through 10,000 different steps.
    A textual description can be only part of your question, be sure to provide sample results, errors and your platform in the appropriate forums while asking.

  11. #10
    Calgon is offline Learning Programmer
    Join Date
    Sep 2010
    Posts
    57
    Rep Power
    0

    Re: TUTORIAL: Save time using the PHP include Function <?php include

    It's also wise to consider smarty for something like this, it's great for publicly released content too.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. NASM Include files
    By DarkLordofthePenguins in forum Assembly
    Replies: 2
    Last Post: 09-26-2011, 06:18 PM
  2. C# include file
    By asafe in forum C# Programming
    Replies: 7
    Last Post: 09-20-2010, 01:36 AM
  3. How Do I include this file please?
    By byronwells in forum PHP Development
    Replies: 3
    Last Post: 01-22-2010, 02:32 PM
  4. Include once??
    By Chan in forum PHP Development
    Replies: 2
    Last Post: 02-14-2007, 06:08 AM
  5. Replies: 4
    Last Post: 07-05-2006, 07:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts