HTML and PHP Resources

Short easy to follow tutorials & more…

Introduction to PHP Includes

| 0 comments

Introduction

PHP includes refers to the four statements include, include_once, require, and require_once. These statements allow us to include PHP code written in other files in our current file and execute it as if it was part of our current one.

Example

Lets take a simple HTML page and make it more dynamic with PHP.

example_page.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
$title = "Greatest Page!";
?>
<html>
<head>
<title>
<?php echo $title; ?>
</title>
<style>
header {border-bottom:dotted 1px #AAA;}
footer {border-top:dotted 1px #AAA;}
</style>
</head>
<body>
<header>
<h1>
<?php echo $title; ?>
</h1>
<p>
<a href="#">Home</a> |
<a href="#">About Us</a> |
<a href="#">Contact Us</a>
</p>
</header>
<section>
<h1>Page1</h1>
<p>Hello World!</p>
</section>
<footer>
<p>
<a href="#">Home</a> |
<a href="#">About Us</a> |
<a href="#">Contact Us</a>
</p>
</footer>
</body>
</html>

We can see that this page contains some reoccurring elements. We can take these reoccurring elements and put them in their own files and make it easier to reuse and maintain. For example if in the future you need to add a link to your pages you can edit a single file and the change will be reflected in all your pages, since they will all be pulling the same code from the same file.

Lets go ahead and split the code up into three files.

header.php 

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<html>
<head>
<title>
<?php echo $title; ?>
</title>
<style>
header {border-bottom:dotted 1px #AAA;}
footer {border-top:dotted 1px #AAA;}
</style>
</head>
<body>
<header>
<h1>
<?php echo $title; ?>
</h1>
<p>
<a href="#">Home</a> |
<a href="#">About Us</a> |
<a href="#">Contact Us</a>
</p>
</header>

home.php 

PHP
1
2
3
4
<section>
<h1>Page1</h1>
<p>Hello World!</p>
</section>

footer.php 

PHP
1
2
3
4
5
6
7
8
9
<footer>
<p>
<a href="#">Home</a> |
<a href="#">About Us</a> |
<a href="#">Contact Us</a>
</p>
</footer>
</body>
</html>

Now that we have the code split up this way, we can reuse the code for our header and footer sections.

Including the Content

We have split the code up and now we are ready to use our methods, include, include_once, require and require_once

These methods simply include the code of the specified location to our current file. for example, let create a new file, landing.php with the following code:

landing.php 

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Variable is used in our header
$title = "Greatest Page!";
include 'header.php';
?>
<section>
<h1>Welcome!</h1>
<p>Hola! Hello! Hallo! Salut!</p>
</section>
<?php
include 'footer.php';
?>

If you take a look at the final rendering of the page, you will notice it is the same as the original. These functions allow us to include code from other sources.

Its also important to note that variables can be used across included files, as long as it has been declared before the line it is used on. In our example we declared the variable $title and used it in our header file.

Syntax

These statements can be used with or with out parenthesis.

PHP
1
2
3
include 'path/to/file.ext';
include('path/to/file.ext');

PHP
1
2
3
include_once 'path/to/file.ext';
include_once('path/to/file.ext');

PHP
1
2
3
require 'path/to/file.ext';
require('path/to/file.ext');

PHP
1
2
3
require_once 'path/to/file.ext'
require_once('path/to/file.ext');

The path provided can be absolute or relative to the current directory or the include path specified in php.ini.

Including vs Requiring

These two constructs do the same thing and are interchangeable for the most part. The real difference is in the way that they handle errors.

Include issues a notice and execution of the script continues if the path of the specified file is not found.

Require issues an error and execution of the script stop at that point.

Do it just once

The two constructs require_once and include_once also do the same as require and include although as the name suggest they only do it once.What I mean by this is that if you had written two include statements in your script at different point for inclusion of the same file, the file would be included twice and the code in that file would be executed twice.

With the require_once and include_once constructs the PHP parser ignores any subsequent request for inclusion from require_once and include_once.

It should be noted that any inclusion request using include and require will be fulfilled normally. Use the constructs require_once and include_once to include any files that define functions, classes or other code that you do not want to be re-declared or executed a second time.

Summary

The constructs discussed here are simply a way to include contents of another file into the current one. The contents of the included files will be executed and displayed as if they had been written in the file that including it. Variables declared before the inclusion can be used in the included file, and variables, functions and classes declared inside the included file can be used in the file that including it.

Leave a Reply

Required fields are marked *.