Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Including files

David Carr

Tutorials PHP & MySQL

<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. 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!


My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.