Using PHP includes

Using PHP includes

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on March 5, 2009

PHP is a powerful language that has replaced HTML frames and JavaScript includes in performing the task of including code from one page onto another. Generally PHP includes are used for database connection scripts, functions or classes, to be included in dynamic pages. But they can also be used in your static html projects too.

This is really useful for including anything from a header, footer, menu or a script across multiple pages. If you then need to make a change to the header, footer, menu or script, then you only need to edit the included file.

How to use it

Its really simple to get started with includes. If you want to include a menu in your web project simply add the line of code below to your site pages.

<?php include ('includes/menu.php'); ?>

You then need to create the includes folder on your server and paste the following code or what ever script you want.

When you create the menu.php page there is no need to have the html doc type head and body script in there just your menu code, this is because php will include the complete file ‘as is’ and you will have two head and body tags.

<a href="http://www.papermashup.com/index.php">Home</a> /
<a href="http://www.papermashup.com/about.php">About Us</a> /
<a href="http://www.papermashup.com/links.php">Links</a> /
<a href="http://www.papermashup.com/contact.php">Contact Us</a>

So includes can be a great time saver when creating generic content throughout a site, this has been a simple introduction on how to get started using them.

More tutorials from Papermashup
Comments
4 discussions around Using PHP includes
  1. includes saves so much time updating your sites, I use then whenever poss.

  2. Nick says:

    Isn’t it better to use require on db connects and that sort of thing?

    • Ashley says:

      @nick The two functions are identical in every way, except how they handle errors. The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).

  3. Brian says:

    Wow, awesome, I have been using iframes! Much simpler lol

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.