Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.
Menu
PHP TUTORIALS


PHP Include

The include() function allows you to include another file into your current script. This is useful if you want to reuse the same code over & over again, for a header and footer for your webpages, for example.

You can use the include() function like this: <?PHP

include('header');

echo 'The individual page contents go here';

include('footer');

?>
This will include your default header & footer that has been stored in separate files called header.php & footer.php. If your header & footer are always the same, and only the middle content changes, this is a great way to prevent you having to manually type the header & footer on every page.
Also, if you ever need to make a change to your header or footer, you only have to edit the header.php & footer.php pages, and those change will apply to every page that includes them.

Or you can use the include_once() function like this: <?PHP

include_once('header.php');

?>
The include_once() function is slightly different from the normal include() function, in that it remembers whether the file you're including has already been included, and doesn't include it again if it has.
If you accidentally type include('header.php'); twice in your script, you would get 2 headers printed out on your page. If you used include_once('header.php'); instead, you would only get one header printed out, even if you'd accidentally typed it out 2,3,4 times!


A slightly different type of include is the require() & require_once() functions.
They work in exactly the same way as include() & include_once(), except that your script will not continue processing if the file requested inside a require() is not available.
<?PHP

require('header.php');

?>
<?PHP

require_once('header.php');

?>

Title