Includes

  • Description : In this tutorial you will learn how to include multiple files within PHP pages!
  • Language : PHP
  • Date Added : 11th November, 2009
Bookmark and Share

To make a website more functional and easier to maintain you need to use includes. This allows you to edit one file which will update any page which includes that file. Below is an example of a typical file that is included:

Menu.php

Code :
<a href="/linkOne.php">Link 1</a> <a href="/linkTwo.php">Link 2</a> <a href="/linkThree.php">Link 3</a>

Now if you were to include that menu.php file within an index.php file:

Code :
<?php //put this line where you want to include the file include("menu.php"); ?>

If you were to then view source you would see:

Output:

If you use an include link and the file does not exist it will only throw a warning message. If you want to stop the page running when a file cannot be found use require:

Code :
<?php //put this line where you want to include the file require("menu.php"); echo "IT WORKED!"; ?>

In the case above the echo statement would never appear, as the code will stop running at that point.

Sometimes you might want to include a file that is a level higher, so you might be in http://www.yourdomain.com/subfolder/file.php and you want to include a file found at http://www.yourdomain.com/menu.php you will need to use:

Code :
<?php //put this line where you want to include the file include("../menu.php"); ?>

For every 'level' you wish to go uo you need to use "../". This concludes another tutorial, hopefully there will be some scripts available for use soon!



Login Required

To post comments you must sign in to your account below!

User :
Pass :

If you do not currently have an account click here to create one now!