PHP File Include

The include (or require) statement adds all of the text, code, and markup from the given file into the file that uses the include statement.

When you want to put the same PHP, HTML, or text on more than one page of a website, including files is a great way to do it.
There are two PHP functions that can be used to add a PHP file to another PHP file.

  1. include() Function
  2. require() Function

include and require statements in PHP

With the include or require statement, you can put the code from one PHP file into another PHP file before the server runs it.

The include and require statements are the same, except for what happens if they both fail:

require will cause a fatal error (E COMPILE ERROR) and stop the script. include will only cause a warning (E WARNING) and the script will keep running.

So, use the include statement if you want the execution to continue and show the output even if the include file is missing. Otherwise, use the require statement to add a key file to the flow of execution in FrameWork, CMS, or a complex PHP application. This will help protect the security and integrity of your application in case one key file is accidentally deleted.

By adding files, a lot of work is saved. This means you can make a header, footer, or menu file that will be used on all of your web pages. Then, if you need to change the header, you only need to change the header include file.

Syntax

include ‘filename’;

or

require ‘filename’;

 

The function of include()

The include() function copies all of the text in a given file into the file that uses the include function. If there is a problem loading a file, the include() function will issue a warning, but the script will still run.

Let’s say you want to make a menu for your website that everyone can use. Then add the following to a file called nav.php.

Example

<a href=”http://www.coderazaa.com/index.php”>Home</a> –
<a href=”http://www.coderazaa.com/HTML”>HTML</a> –
<a href=”http://www.coderazaa.com/ajax”>AJAX</a> –
<a href=”http://www.coderazaa.com/PHP”>PHP</a> <br />

Now, make as many pages as you want and add this file to each of them to make a header. For example, your header.php file could now contain the following.

<html>
<body>

<?php include(“header.php”); ?>
<p>Example to show how we can include PHP file!</p>

</body>
</html>

output

include example

 

The function of require()

The require() function copies all of the text in a given file into the file that uses the include function. If there is a problem loading a file, the require() function will produce a fatal error, which will stop the script from running.

So require() and include() are the same, except for how they handle errors. Use the require() function instead of the include() function because scripts shouldn’t keep running if files are missing or have wrong names.

You can use the same example with the require() function, and the same thing will happen.

 

Example

<a href=”http://www.coderazaa.com/index.php”>Home</a> –
<a href=”http://www.coderazaa.com/HTML”>HTML</a> –
<a href=”http://www.coderazaa.com/ajax”>AJAX</a> –
<a href=”http://www.coderazaa.com/PHP”>PHP</a> <br />

Now, make as many pages as you want and add this file to each of them to make a header. For example, your header.php file could now contain the following.

<html>
<body>

<?php require(“header.php”); ?>
<p>Example to show how we can require PHP file!</p>

</body>
</html>

output

include example

 

 

But if you try to add file where the file does not exist, you will get error result.

 

include vs. require in PHP

With the require statement, you can also add a file to PHP code.

But there is a big difference between include and require: If a file is included with the include statement, but PHP can’t find it, the script will keep running:

If we do the same thing with the require statement, the echo statement won’t be run because the script stops running when the require statement returns a fatal error:

Use require when the application needs the file to work.

Use “include” when the file isn’t needed and the programme should keep going if it can’t be found.

People also search

Leave a Comment

Scroll to Top