PHP include VS require: explained with different examples

The purpose of include and require statements in PHP

The include and require PHP statements are used to include the specified files in the calling file/program.

For example:

include("user_auth.php");

 

require("scripts/db_connect.php");

The code of the specified file will be copied to the calling program before execution by using the include or require statements.

This feature is particularly useful if you have to use the same code of PHP, HTML, etc. in multiple pages. For example, you have to establish a connection with MySQL or some other database from 100s of pages. You have to use the same piece of code to connect to a database server.

Instead, you may create a file e.g. db_connection.php, and write the code of the DB connection in it. Juts use the include or require statements in those 100s of pages and include that db_connection.php file. Not only it will simplify the code if in the future DB parameters are changed like user_id, password, etc. You have to change this only in the db_connection.php file.

Similarly, you may use one file for the left, header, and footer bars/menus or widgets across the website by using include or require statements.

The only difference between include and require statements is described below.

In case of include statement:

If the specified file is not found, the program will generate a warning message. In that case, the execution of the rest of the program will continue and users will see only a warning message (if settings are made in the configuration file). This may be useful in case the included file does not impact the other program sections.

In case of require statement:

If a given file is not found after using the PHP require statement, the program will generate a fatal error (E_COMPILE_ERROR). It will halt the script. So, the require statement may be used in scenarios where other sections of the program are dependent on the included file. For example, it contains DB script that connects to the database and fetches the rows from a MySQL table. This data is displayed in an HTML table.

The following section demonstrates the ways to include files by using PHP include and require statements.

An example of using PHP include statement

In this example, two PHP files are created. The first file, define_variables.php contains two variables. The other file calls this file and displays the variable values by using the echo statement. Both files are placed in the same directory.

PHP code and markup:

<!doctype html>
<html>

<head>

<style>
#timerdiv {
  background: green;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div id="timerdiv"><h3>The demo of PHP include</h3>
< ?php 
include("define_variables.php");

echo "The value of variable x =". $x;
echo "<br>The value of variable y =". $y;
?>
</div>

 
</body>
</html>

Sample output:

PHP include

This is how the include statement of PHP is used:

include("define_variables.php");

As included and calling files are placed in the same directory, you simply need to use the file name to include file.

An example of including an inner directory file

In this example, the same file containing the variables is stored in an inner directory, “scripts”. This is how the define_variables.php file can be included.

PHP and markup:

<!doctype html>
<html>

<head>

<style>
#timerdiv {
  background: #408080;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div id="timerdiv"><h3>PHP include with inner directory</h3>
< ?php 
include("scripts/define_variables.php");

echo "The value of variable x =". $x;
echo "<br>The value of variable y =". $y;
?>
</div>

 
</body>
</html>

Sample output:

PHP include inner

The following PHP code is used to include the inner directory:

include("scripts/define_variables.php");

After including the file, the included file variables are used just like in above example:

echo "The value of variable x =". $x;

echo "<br>The value of variable y =". $y;

An example to include a backward directory file

This time, the calling file exists in the inner directory. This is how you will include a file:

<!doctype html>
<html>

<head>

<style>
#timerdiv {
  background: #408080;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div id="timerdiv"><h3>PHP include with calling file in inner directory</h3>
< ?php 
include("../define_variables.php");

echo "The value of variable x =". $x;
echo "<br>The value of variable y =". $y;
?>
</div>

 
</body>
</html>

Output:

PHP include calling inner

The include statement in above example used as this:

include("../define_variables.php");

To go one step back use “../” before the file name.

Using require statement to include files example

In all above examples, simply replace the word include by require to execute the require statement of PHP.

Using the same PHP file as in above example, this is how the require statement will work:

<!doctype html>
<html>

<head>

<style>
#timerdiv {
  background: #800000;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div id="timerdiv"><h3>A demo of PHP require statement</h3>
< ?php 
require("define_variables.php");

echo "The value of variable x =". $x;
echo "<br>The value of variable y =". $y;
?>
</div>

 
</body>
</html>

Output:

PHP require

The require statement is used as follows:

require("define_variables.php");

 

What is the output if target file does not exist with include statement?

As mentioned earlier, if the specified file does not exist, the include statement will generate a warning message. The program will keep on running or would not halt.

In this example, I mistyped the above file name and see the output (first in case of include statement):

<!doctype html>
<html>

<head>

<style>
#timerdiv {
  background: green;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div id="timerdiv"><h3>Output as File does not exists</h3>
< ?php 
include("define_variables12.php");

echo "The value of variable x =". $x;
echo "<br>The value of variable y =". $y;
?>
</div>

 
</body>
</html>

Output:

PHP include warning

You see, I called the define_variables12.php file that does not exist and it simply generated a warning message. While the echo statement from the calling program still executed and displayed:

The value of variable x =

The value of variable y =

Now see what happens in the case of require statement:

<!doctype html>
<html>

<head>

<style>
#timerdiv {
  background: #FF0000;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div id="timerdiv"><h3>Output as File does not exist</h3>
< ?php 
require("define_variables12.php");

echo "The value of variable x =". $x;
echo "<br>The value of variable y =". $y;
?>
</div>

 
</body>
</html>

Output:

PHP require fatal error

The following line generated a fatal error:

require("define_variables12.php");

 

Also, notice in the output, it did not display the echo statement messages as in the case of include statement. Instead, the program was halted.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!