Tutorials, PHP & MySQL

Including files

<p>Including files with php is very easy to do. This tutorial shows you how.</p>

<p>The main reasons why you might use php includes/require one of these reasons is to include the footer in to every page but only have one footer.</p>

<p>Let me explain you create a new file in can be a .html or .php extension either will work I always choose .php extension as I usually has some php running even in the footer.</p>

<p>In your new file save it as footer.php and in the file only have what you want in the footer do not put in the doc type or &lt;html&gt;&lt;title&gt;..ect as you will already have this information in your other pages that this footer will be included with.</p>

<p>this is an example of a footer file:</p>

<pre lang="php">
<div id="footer">Copyright &copy; your name <?php echo date('Y');?> </div><!-- close footer -->

<p>This is the full footer.php page. When its included into another page and you look at the source code it looks like its all one page.</p>

<p>Okay so now you can see why including a file is useful. But before going into how to include a file you need to decide if you want to use includes or requires.</p>

<p>Include VS Require</p>

<p>When including/requiring a file as long as the file is found there is no difference in these two methods. But if the file cannot be found they act very differently.</p>

<p>Includes will show you an error if the file is not found and will continue loading the rest of the page, But if your using require and the file is not found you will see an error message and nothing else below where the error message occurs.</p>

<p>The reason for this is if a require cannot find the file it is looking for it stops the rest of the page/script from loading.</p>

<p>So if your page needs to connect to a database and your want all pages to link to the database connection page then use require. In most cases you can use includes. Its your preference which method you use.</p>

<p>How to includes a file with includes or require</p>

To include a file you would write the following code:

<?php include ('footer.php'); ?>
//or if your included file is in another folder:
<?php include ('folder-name/footer.php'); ?>

First we open php with <?php the use the function include () between the parenthesis is where the file is located, So if the file is in the same folder as the page using the include you would use ('filename.php') then remember the semicolon on the end ';' then close php with '?>'

Using require is almost the same:

<?php require ('footer.php'); ?>

//or if your required file is in another folder:

<?php require ('folder-name/footer.php'); ?>

The only difference in the code is instead of using include were using require. 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.