PHP Include File

Summary: in this tutorial, you will learn how to include code from a script file inside another script file using PHP include functions.

The PHP include functions allow you to create modular and reusable code. PHP provides four functions that help you to organize the script files into modules and reuse them in another script file. Those functions are

Those functions are include(), require(), include_once() and require_once(). We will examine each function in greater detail.

PHP include a file

PHP include() function allows you to include code from a script file inside another script file. By using the include() function, we can break our code into multiple script files e.g., header.php, footer.php, functions.php and index.php files.

  • header.php contains code that displays the header of the site.
  • footer.php contains code that displays the footer of the site.
  • functions.php contains common functions that can be reused in other script files.
  • index.php contains code for displaying a complete site.

The following is the source code of the corresponding file:




In the header.php and footer.php files, we included the functions.php file so that we can use the display_header() and display_footer() functions.

PHP provides the require() function that works the same as the include() function with a slight difference. The difference between the include() function and the require() function is that if the file to be included cannot be found, the include() function raises a PHP warning while the require() function raises a fatal error and terminates the script.

Suppose we want to use the header.php and footer.php files, and functions from the functions.php file in the index.php file. Let’s try to include them all in the index.php as follows:

If you run the script, you will get an fatal error saying that you cannot redeclare display_header() function. The following diagram illustrates how we organized the files using the include() function:

php include file

The functions.php file that contains the display_header() function was included twice, one in the header.php file and one in the index.php file, therefore we got a fatal error.

To overcome this problem, PHP provides a pair of useful functions to allow you to include a file only once.

PHP include a file only once

To include a file only once, you use the include_once() or require_once() function. The difference between the include()  function and the include_once() function is that the include_once() function includes a file just only once. If you call the include_once() function to include the same file again, nothing happens.

So we can fix our issue by using the include_once() function as follows:




PHP include paths

PHP include path provides a convenient way to track the location of files to include. The include_path directive contains a list of paths that PHP uses to search for files to include. Each path is separated by a colon(:) or a semicolon (;) , depends on the platform where you deploy PHP.

To get the current include path, you use the get_include_path() function as follows:

To set a new include_path, you use the function set_include_path() as follows:

After calling the set_include_path() function, you can include a file in the code as follows:

When the script executes, PHP looks for the functions.php file in the same directory as the script file. If the functions.php file cannot be found, PHP searches for the file in the include_path directory that we have set e.g., /www/demo/lib before.

By using the include_path, your code is more tidy and portable.

PHP dynamic includes

You can call include() or include_once() function inside a control flow statement such as if-else or switch statement to include a file based on a particular condition. The following example demonstrates how to include a file in PHP dynamically:

In this tutorial, we  have discussed about PHP include files using various useful PHP functions. We also covered the include_path directive and how you could use it to make your code more portable and tidy.

  • Was this tutorial helpful ?
  • YesNo