PHP Includes

The poll on the footer of my site keeps rising for HTML/CSS tutorials… and although this isn’t a HTML or CSS tutorial it should help a lot of people out who are still only using HTML and CSS. I speak to you all about the heavenly PHP includes. You don’t really need to know anything about PHP to use these they are VERY VERY simple!

Description: “The include() statement includes and evaluates the specified file.” PHP Manual

This basically means putting one file into another; it’s sort of like using iframes but without the horrible scrollbar.

The most common use for PHP includes are layouts. In almost all layouts you have a basic design that doesn’t change when you go between pages (it is always best have consistency within a site it helps the user navigate, and also provides a branding for your website).

So say you have a website with 10 pages in it… well that’s 10 page you need to write the (copy and paste) the layout onto! That’s 10 pages way to much! It’s much easier to write it out once and then within the pages use this PHP script to call the one layout.

All you need to know is this tiny piece of code:

<?php include_once ("includes/FILENAME.php")?>

Not much is it! Lol there are two ways of using the PHP includes

  • Include_once : this basically means no matter how many times the snippet is written out on the page it will only display the content once and any other reference to it will be ignored (this is mainly used for loops as a failsafe but it is good practice to get into and it saves any mistakes in case you accidentally write it again.
  • Include : well it’s the opposite of include_once this allows you to include the file in as many times as you like, I can’t think of a reason anyone would want to, unless you’re calling equations etc and you need to input the same data in more than once but for layouts and minor PHP I would always use include_once

Below is an image that will help you to cut up your page into includes, and remember to never nest any of your tags that is a sure way to break your site.

Document before includes
Side note: it is always a good idea not to include the body tags or HTML tags within the include just in case something happens to the include say someone stops your page from loading properly or PHP on your sever crashes you will not end up with an blank page your unique content will still be visible for users to see and search engines to scan.

Document After includes have been applied

If you need any help with this please comment below and I will try my best to help you out.

  • Great tip, Dee 😉
    BTW – thanks for the comment on my site! glad you liked it
    there’s a new update over at BM 😉 Check it out.

  • Ohh! Thanks for this Dee!
    This will help a alot! ?

  • Dee

    No problem I remember how long it took redesigning before includes and sometimes at work I have to edit other peoples sites and it’s so time consuming changing one small thing on every page when it can be easily done with includes I just hope this helps others out and if you ever have problems implementing it don’t hesitate to contact me

  • Wow, fantastic tut, Dee! Quite an ingenious way to eliminate the “copy & paste” issue with multi-paged layouts 🙂 Thanks for sharing!

  • Hi Dee! Just wanted to thank you for the halloween art! that was very nice and thoughtful of you to take the time to make it! and i laughed really hard, it’s really fun 😛

    Cool tutorial! I’m not sure if you have any tutorials on how to include a wp-blog on the index page of a website but hopefully i’ll have more time later to browse through your tutorials. if you have any suggestions for sites explaining how to do that in an easy way, that would be great.

    have a wonderful day 🙂

  • Dee

    Hey Jasmin, I don’t have a tutorial at the moment on how to do that but if you tell me a little bit more I could come up with something, I’ve only started to get into doing tutorials so not that many up at the moment.

    Your site at the moment is run off wordpress, if you could explain a little more about what you want to achieve becuase I think at the moment you have your blog on your index. The best way is to use wordpress as it is intended to be used, you can put wordpress onto an existing site using an rss feed to feed the data on to an external site, but you lose a lot of the features and functions that you get with wordpress.

  • Kitty

    nice tut chica

  • Definitely going to look into this when I bring my sites back if I can’t get wordpress on the server. I used to have a “cheat” to this code and I loved it (apparently I couldn’t run regular PHP on our old server.. don’t ask why, I have no clue.. so my brother wrote a cheat that means I could use the include (but not written in php) anyway. Loved that feature and I want it back. When I re-launched PAAP I just wrote the whole thing in HTML and ugh, that annoyed me so much! (I hate my menu now! *lol*)

    Might poke you for advice in the future if you don’t mind. You’re like the PHP-yoda ;D

  • Dee

    Hey Dana, how did you run PAAP on your old server if you couldn’t run PHP, and doesn’t this new server run regular PHP if you’ve got gallery2 to work?

    I’d be glad to help in anyway that I can 🙂 although I’d say I’m more like Darth Vader I’m usually told off at work for doing things in a really hacky way lol.

  • You know, I hadn’t even thought of that part. Hmm.. I think my dear brother has been yanking my chain to make his own life easier *lol* But I know the new server is ok with PHP which is why I’m planning on trying my hand at it and see if I can figure it out.. scares the bejesus out of me though *lol* 😉

    Haha! That’s seriously awesome! I wanna come to the dark side XD
    And thank you for being open to my badgering and constant annoyance 😉 Will be reading through this tutorial again when I sit down and really decide to code and then see what happens. Also gonna check out w3school, they’ve been a huge help over the years as I’ve figured out CSS 🙂

  • Dee

    Don’t go to w3school! there are really out dated!! you wanna try places like http://css-tricks.com or http://www.lynda.com/search?q=css&x=27&y=16

    w3school teaches bad practice its fine for learning the basics but your well beyond that! try those two above so much better!.

  • Oh, ok! Thanks for the links 🙂 Had no idea they were that bad over at w3school, always heard good things about them not to mention I’ve gotten help there a lot over the years but again, just to refresh or learn basic things and tags. Will abandon them now though 🙂