PHP include and require functions

The PHP include and require functions are used to include other PHP files in a PHP file. For example, a webpage which needs to have a header, content section, and footer can be made into three PHP files – the main file can then include the header and the footer files in it. This is useful if you have PHP code that needs to be included in many pages across a site and can save time coding as well as reducing the risk of human error when repeating code.

The include and require functions work similarly but do have a key difference. The include function will include a file but the rest of the code will not break if the other file can’t be found or loaded. On the other hand, the require function is used when another file is required but will break the rest of the page if it can’t be found or loaded.

Watch the video below and then scroll down to see the sample code.

Sample code

There are a few files involved here. You can also download the background image and CSS code used for the template.

Step 1: Create an index.php file and add the following code.

<html>
<head>
  <title>Home page</title>
 	<meta charset="utf-8"/>
 	<link rel="stylesheet" href="theme.css" type="text/css"/>
<body>
  <?php include 'header.php'
  // can also use require which does the same thing as include
  // however, if using require and the required file is missing,
  // errors will result in none of the rest of the page loading whereas
  // include will not kill the rest of the page.
  ?>

  <div id="content">
 	<h2>Welcome to my website</h2>
 	<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
 	<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
  </div>

  <?php include 'footer.php'?>
</body>

</html>

Step 2: Create a header.php file and include the following code:

<html>
<head>
</head>
<body>
 
<div id="header">
 <h1>My website</h1>
</div>
 
<div id="nav">
  <ul>
     <li><a href="index.php">Home</a></li>
     <li><a href="about.php">About</a></li>
     <li><a href="products.php">Products</a></li>
     <li><a href="support.php">Support</a></li>
     <li><a href="contact.php">Contact us</a></li>
  </ul>
</div>
</body>
</html>

Step 3: Create a footer.php file and include the following code.

<html>
<head>
</head>
<body>
<div id="footer">
 Copyright....2015.
</div>
</body>
</html>

Step 4: Create a theme.css file and include the following code. Change the CSS styling as you wish.

#header{
 background-image:url("background.png");
 background-color:#334a94;
 height:80px;
 color:#FFFFFF;
 text-align:left;
 padding-left:20px;
 padding-top:10px;
 font-family:Arial;
}
 
#nav{
 background-color:#334a94;
 height:30px;
 padding:5px;
}
 
#content{
 width:500px;
 padding:10px;
}

h2{
  font-family:"Tahoma";
}

p{
  font-family:"Palatino Linotype";
}
 
#footer{
 background-color:#334a94;
 color:#FFFFFF;
 clear:both;
 text-align:center;
 padding:20px;
}


ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 100%;
    background-color: #334a94;
}

#nav li{
    display: inline;
}

li a {
    
  font-family:Calibri;
  font-size: 14pt;
  font-style:none;
    color: #FFFFFF;
    padding: 8px 16px 8px 16px;
    text-decoration: none;
}

li a:visited {
    background-color: #4CAF50;
    color: #FFFFFF;
}

li a:hover {
    background-color: #786ED4;
    color: #000000;
}

Step 5: Download this image and include it in the same folder along with the three PHP files and one CSS file. You can add more pages (eg. about.php or products.php) and include the header and footer in each file.