PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Include and Require

In this chapter, we will learn how to include the content of a PHP file into another PHP file.

Why Including? (or Requiring)

Let's assume that you have a simple blog which consists of three main parts; header, body, and footer. If you needed to change the header of each page are you going to open all the files for each page and update the header? No, you will need to keep the header in a single file and include it in the other files.

PHP Including Example
PHP Including Example

Simply, including and requiring is used to reuse PHP files. It makes handling projects very easy as we can change the layout with just changing a single file.

How to Include Files? (or Require)

There are 4 statements in PHP that is used for this purpose.

include vs require

The include and require statements are identical except their error handling behavior when the file was not found.

  • include produces a warning and continues execution of the script
  • require produces a fatal error and stops the execution of the script

If the included script is needed to do the next processes, you should use require.

_once

By adding _once to include or require, you can give a new rule for those statements; "Include the file only if it is not already included".

For instance, when you use require_once, it checks if that file is already required (or included) in the current script. If no, the file will be required. Otherwise, the statement will be skipped.

It is a good practice to use require_once, as it stops the script if the file is not found and it will only require a file once. But, this depends on your need. If you need to include the same file multiple times, you should use include or require.

Including is frequently used when working with class files. We will learn more about that in Object-Oriented Programming chapters.

How to use Include and Require?

For this example, we will create template files to include. (header.php and footer.php)

header.php


<nav>
	<a href="/">Home</a>
	<a href="/tutorials">Tutorials</a>
	<a href="/tutorials/php">PHP Tutorial</a>
</nav>

footer.php


<footer>
	with ♥ by Hyvor © <?= date('Y') ?>
</footer>

1. include

PHP include Example


<html>
<body>

	<?php include 'header.php' ?>
	<p>I'm using the include statement</p>
	<?php include 'footer.php' ?>

</body>
</html>

Run Example ››

2. require

PHP require Example


<html>
<body>

	<?php require 'header.php' ?>
	<p>I'm using the require statement</p>
	<?php require 'footer.php' ?>

</body>
</html>

Run Example ››

3. include_once

PHP include_once Example


<html>
<body>

	<?php include_once 'header.php' ?>
	<p>I'm using the include_once statement</p>
	<?php include_once 'footer.php' ?>

	<?php include_once 'footer.php'; // this has no effect ?>

</body>
</html>

Run Example ››

4. require_once

PHP require_once Example


<html>
<body>

	<?php require_once 'header.php' ?>
	<p>
		I'm using the require_once statement for header and include for footer. 
		No matter what we used, require_once checks if the file was included or required earlier. 
		So, last require_once has no effect
	</p>
	<?php include 'footer.php' ?>

	<?php require_once 'footer.php'; // this has no effect ?>

</body>
</html>

Run Example ››

Tips on Including (or Requiring)

  • All the above statements can be used with parentheses as they are statements. (Remember what you learned in the output chapter.)
    
    <?php
    include('header.php');
    require('header.php');
    include_once('header.php');
    require_once('header.php');
    
    
  • When using _once statements, it doesn't matter if you previously included or required the file. See the last example above.
  • Including can be done starting from the document root. (using $_SERVER['DOCUMENT_ROOT'])
    
    <?php
    require_once $_SERVER['DOCUMENT_ROOT'] . '/folder/file.php';
    
    
  • Including can be done with relative paths.
    
    <?php
    require_once __DIR__ . '/file-in-this-folder.php';
    require_once __DIR__ . '/../file-in-previous-folder.php';
    require_once __DIR__ . '/../../file-in-two-folders-back.php';
    
    
    Note: __DIR__ is a constant defined by PHP which holds the absolute path of the current file's directory. /../ can be used to go to previous folders.
  • All the included files are regarded as PHP files and any PHP code inside it will be executed. So, you should never include any file that uploaded by the users. For example, if you let your users to upload a .css file, and if someone inject PHP code into that, all the code will be executed if you include or require those files. That's a security risk!
Facebook Twitter