Introduction To PHP Include & Require

By .     What do you think of this article?

The PHP Include function is used to pull different pages together into one. For example, a default header, menu, and footer can be included on multiple content pages. When you want to update the menu, you only need to update a single file. Then the changes are reflected on all content pages that include the menu.

In order to use Include, you first need PHP installed on your web server (almost certainly the case). At least PHP version 4 will do, although your host should really be using version 5.3 or newer by now. Once that’s settled, open up your favorite text editor and let’s get to work. I prefer Notepad++, but most any will do, even basic Notepad built into your operating system. Don’t use Microsoft Word!

A couple quick notes to remember:

  1. 1.) The content page is the one people will be going to, such as about.php or article1.php, not menu.php.
  2. 2.) Only the content page needs to be a .php file because that’s the only one with PHP statements. In my example below, all files are PHP files because it’s good practice and I’m used to it. As your website grows, you’ll likely include more PHP, perhaps to your header, menu, and footer, too.

header.php

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>About</title>
<style>
  #container {width:60%; margin:0 auto}
  #header {text-align:center}
  #menu {margin-top:0; margin-right:5%; float:left}
  #footer {text-align:center; clear:both}
</style>
</head>
<body>
<div id="container">

menu.php

<ul id="menu">
  <li>Menu 1</li>
  <li>Menu 2</li>
  <li>Menu 3</li>
</ul>

footer.php

<div id="footer">Copyright info. 2013.</div>
</div><!-- #container -->
</body>
</html>

about.php

<?php include 'header.php'; ?>
<h1 id="header">About Page</h1>
<?php include 'menu.php'; ?>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Sed imperdiet diam vel eros sagittis sit amet hendrerit erat auctor.</p>
<p>Morbi lobortis lorem lobortis orci fringilla sit amet volutpat augue mollis.</p>
<?php include 'footer.php'; ?>

Require Function

The Include and Require functions are almost exactly the same, where in the difference applies to when they fail to work. Using the above example, let’s say the header.php file fails to load. Maybe it’s incorrectly linked to, or maybe it’s processing a shopping check-out system and thinks you’re trying to skip a step. You wouldn’t start at the confirm order screen; you still need to add items to the shopping cart, then add shipping and payment information.

If the header file fails to load and it’s linked to with Include, the rest of the content will still output to the browser. menu.php, about.php, and footer.php will still display their content, or at least as well as they can without anything from header.php. You’ll see an error message (E_WARNING) about the header file failing to load, but at least you’ll get some content to display. However, depending on how you want the website to run, you might not want that at all.

Require ensures that all 3 supplement files are properly loaded before any output is done. If even one fails, nomatter which one, about.php won’t display anything but an error message (E_COMPILE_ERROR). Here is how you write that About Page using the Require function:

<?php require 'header.php'; ?>
<h1 id="header">About Page</h1>
<?php require 'menu.php'; ?>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Sed imperdiet diam vel eros sagittis sit amet hendrerit erat auctor.</p>
<p>Morbi lobortis lorem lobortis orci fringilla sit amet volutpat augue mollis.</p>
<?php require 'footer.php'; ?>

Include_once & Require_once Functions

There may be situations in more complex scripts where you utilize a specific file multiple times in order to process information. The Include_once and Require_once functions work just like their counterparts, but they’ll only function the first time they’re called. In the below example, the about page will only output the menu once.

<?php include 'header.php'; ?>
<h1 id="header">About Page</h1>
<?php include 'menu.php'; include_once 'menu.php'; ?>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Sed imperdiet diam vel eros sagittis sit amet hendrerit erat auctor.</p>
<p>Morbi lobortis lorem lobortis orci fringilla sit amet volutpat augue mollis.</p>
<?php include 'footer.php'; ?>

Resources

Open up the PHP Manual for a more technical look at these functions and keep up to date as PHP changes: Include, Include_once, Require, Require_once

Posted in: Web Development



is the site owner of Computer Tech Tips and is passionate about computer technology, particularly Windows-based software, malware removal, and web development. He enjoys helping people troubleshoot computer problems and providing technical support to family, friends, and people around the net. Xps wrote 78 article(s) for Computer Tech Tips.


 Subscribe to comments: this article | all articles
Comments are automatically closed after a period of time to prevent SPAM.