php

File Inclusion in PHP

When a PHP script is required to use inside multiple PHP files, it is better to write a PHP file with the common script and include this file inside other PHP files where that script requires. There are two main benefits of file inclusion. One is code reusability and the other is making the code modification easier. For example, the PHP script for database connection is required by those PHP files where the data from the database is required to read, write, update, and delete. In this case, it is better to write a PHP file with the database connection script and include this file inside all required PHP files. PHP has four built-in functions for file inclusion. These are include(), include_once(), require(), and require_once(). The uses of these functions are shown in this tutorial.

Using Include() and Include_Once() Functions

Both include() and include_once() are used to include the file inside a PHP script. The difference between these functions is that if the same file is included multiple times in the script, the include() function includes the file multiple times. But include_once() includes the file for one time only. Both functions return a warning if the included file does not exist.

Syntax:

  1. include ‘filename’ or include (‘filename’)
  2. include_once ‘filename’ or include_once (‘filename’)

Both include() and include_once() functions take the inclusion filename as the argument.

Create a File for Inclusion

Create a PHP file named marks.php with the following script that initializes three variables:

<?php
 //Initialize passing marks
 $mcq_passing_score = 60;
 $written_passing_score = 50;
 $practical_passing_score = 60;
?>

Example 1: Include an Existing File Using Include

Create a PHP file with the following script that includes an existing file using the include() function. Three numeric values are taken from the URL parameters. These are mcq, written, and lab marks. The minimum passing score for three exams are stored in three variables inside the marks.php file. So, this file is included in this script using the include() function. If the three taken URL values are greater than or equal to the passing score, the “You have passed” message is printed. Otherwise, the “You have failed” message is printed.

<?php

//Check whether three marks are given in the URL address or not
if (isset($_GET['mcq']) && isset($_GET['written']) && isset($_GET['lab']))
{
 //Include the file
 include 'marks.php';
 //Read three marks from the URL address
 $mcq = $_GET['mcq'];
 $written = $_GET['written'];
 $lab = $_GET['lab'];
 //Compare the marks with the passing score
 if ($mcq >= $mcq_passing_score && $written >= $written_passing_score && $lab >= $practical_passing_score)
    echo "<center><h3>You have passed.</h3></center>";
 else
    echo "<center><h3>You have failed.</h3></center>";
}
else
    echo "<center><h3One or more marks are missing.</h3></center>"

?>

Output:

The following output appears after executing the previous script without providing any URL parameter:

The following output appears after executing the previous script by providing three valid marks in the URL:

The following output appears after executing the previous script by providing any invalid mark in the URL:

Example 2: Include Non-Existing File Using Include_Once

Create a PHP file with the following script that includes a non-existing file using the include_once() function. No error or warning is generated by the include() or include_once() function if the non-existence file is included.

<?php

//Check whether three marks are given in the URL address or not
if (isset($_GET['mcq']) && isset($_GET['written']) && isset($_GET['lab']))
{
   //Read three marks from the URL address
   $mcq = $_GET['mcq'];
   $written = $_GET['written'];
   $lab = $_GET['lab'];

   //Include the file
   include_once 'mark.php';
   //Print message
   echo "Checking the passing score..." ;
}
else
   echo "<center><h3>One or more marks are missing.</h3></center>";

?>

Output:

The following output appears after executing the previous script. The filename that is included in the script does not exist but the full script is executed and prints the following message:

Using Require() and Require_Once() Functions

Both require() and require_once() are used to include the files inside a PHP script like the include() and include_once(). The difference between require() and include() functions is that if the included file does not exist, the require() function returns a fetal error and stops the execution of the script. But the include() function continues the execution of the script.

Syntax:

  • require ‘filename’ or require (‘filename’)
  • require_once ‘filename’ or require_once (‘filename’)

Both require() and require_once() functions take the inclusion filename as the argument.

Create a File for Inclusion

Create a PHP file named salary_data.php with the following script that initializes the common salary data in some variables:

<?php
  //Initialize common variables
  $house_rent = 50000;
  $medical = 25000;
  $transport = 20000;
?>

Example 3: Include an Existing File Using Require

Create a PHP file with the following script that includes an existing file using the require() function. The value of $post is taken from the URL parameter and the total salary is calculated based on the $post and the variables initialized in the included file.

<?php
//Include the file
require 'salary_data.php';

//Check post value is given in the URL address or not
if (isset($_GET['post']))
{
   //Read the post value
   $post = $_GET['post'];
   //Assign basic salary based on the post
   if ($post == 'CEO')
      $basic = 80000;
    elseif($post == 'Manager')
       $basic = 60000;
    else
       $basic = 50000;

    //Calculate the total salary
    $total = $basic + $house_rent + $medical + $transport;
    //Print the total salary with the post
    printf("<center><h3>The salary of the %s is %d</h3></center>", $post, $total);
}
else
   echo "<center><h3>The post is not given.</h3></center>";

?>

Output:

The following output appears after executing the previous script without providing any URL parameter:

The following output appears after executing the previous script by providing the post value in the URL:

Example 4: Include Non-Existing File Using Require_Once

Create a PHP file with the following script that includes a non-existing file using the require_once() function. The require_once() function stops the execution of the script for including the non-existence file.

<?php

//Check post value is given in the URL address or not
if (isset($_GET['post']))
{

   //Include the file
   require_once 'salary.php';
   //Print message after including the file
   echo "Calculating the Salary of the post...";
}
else
   echo "<center><h3>The post is not given.</h3></center>";

?>

Output:

The following output appears after executing the previous script:

Conclusion

The way of including the files in the PHP script using include(), include_once(), require(), and require_once() functions is explained in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.