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.
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.
If you need any help with this please comment below and I will try my best to help you out.