How to Include an External File In PHP?

10 minutes read

To include an external file in PHP, you can use the include or require statement. These statements allow you to include the contents of another PHP file into the current file.


Using the include statement:

1
include 'filename.php';


Using the require statement:

1
require 'filename.php';


The include statement will generate a warning if the file could not be found or accessed, but the script execution will continue. On the other hand, the require statement will generate a fatal error and halt the script execution if the file is not found or cannot be accessed.


You can also include files from remote servers or provide a filepath for files located in different directories:

1
2
3
include 'http://www.example.com/filename.php';  // Including files from a remote server (requires allow_url_include enabled in php.ini)

include '/path/to/directory/filename.php';  // Including files from a different directory


It is important to note that the included file is treated as if its code was directly written in the main file, so any variables, functions, or classes defined in the included file will be accessible in the including file. This allows you to modularize your code and reuse common functionality throughout your PHP projects.

Best PHP Books to Read in 2024

1
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

Rating is 5 out of 5

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

2
Murach's PHP and MySQL

Rating is 4.9 out of 5

Murach's PHP and MySQL

3
PHP 8 Objects, Patterns, and Practice: Mastering OO Enhancements, Design Patterns, and Essential Development Tools

Rating is 4.8 out of 5

PHP 8 Objects, Patterns, and Practice: Mastering OO Enhancements, Design Patterns, and Essential Development Tools

4
PHP & MySQL: Server-side Web Development

Rating is 4.7 out of 5

PHP & MySQL: Server-side Web Development

5
PHP Cookbook: Modern Code Solutions for Professional Developers

Rating is 4.6 out of 5

PHP Cookbook: Modern Code Solutions for Professional Developers

6
100 PHP Program Examples | Best for Beginners | PHP Programming Book

Rating is 4.5 out of 5

100 PHP Program Examples | Best for Beginners | PHP Programming Book

7
PHP 8 Programming Tips, Tricks and Best Practices: A practical guide to PHP 8 features, usage changes, and advanced programming techniques

Rating is 4.4 out of 5

PHP 8 Programming Tips, Tricks and Best Practices: A practical guide to PHP 8 features, usage changes, and advanced programming techniques

8
PHP Web Services: APIs for the Modern Web

Rating is 4.3 out of 5

PHP Web Services: APIs for the Modern Web

9
Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

Rating is 4.2 out of 5

Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

10
Programming PHP: Creating Dynamic Web Pages

Rating is 4.1 out of 5

Programming PHP: Creating Dynamic Web Pages


What is the purpose of including an external file in PHP?

Including an external file in PHP serves several purposes:

  1. Code reusability: Including an external file allows you to reuse code snippets or functions across multiple PHP scripts. Instead of duplicating the same code in different files, you can create a separate file and include it wherever needed. This improves maintainability and reduces code duplication.
  2. Modularity: By including separate files, you can split your codebase into smaller, manageable modules. Each module can contain related code that handles specific functionality or tasks. This makes the codebase easier to understand, update, and maintain.
  3. Organization: Including external files helps to organize your codebase. You can separate different aspects of your application into separate files, such as database connections, configuration settings, template files, or libraries. This improves code readability and makes it easier to navigate through project files.
  4. Handling dependencies: By including external files, you can handle dependencies more effectively. For example, if you have a PHP script that requires specific classes or functions, you can include the necessary files to ensure they are available. This facilitates code reuse and ensures that dependencies are properly resolved.
  5. Code separation: Sometimes, it's beneficial to separate presentation logic (HTML/CSS) from the business logic (PHP code). By including external HTML template files, you can keep the markup separate from the PHP code, making it easier to maintain and update both parts independently.


Overall, including external files in PHP improves code organization, reusability, and modularity, making your codebase more maintainable and efficient.


How to include multiple files in PHP using a loop?

To include multiple files in PHP using a loop, you can use an array of file names and iterate through the array using a loop. Here's an example:

1
2
3
4
5
$fileNames = array("file1.php", "file2.php", "file3.php");

foreach($fileNames as $fileName) {
    include_once($fileName);
}


In the above example, an array called $fileNames is created that contains the names of the files you want to include. Then, a foreach loop is used to iterate through each element of the array. Within the loop, the include_once function is used to include the file specified by the current element of the array.


You can add more file names to the $fileNames array to include as many files as you need.


How to include an external file in PHP?

To include an external file in PHP, you can use the include or require statement.

  1. include: It includes the specified file and continues executing the script if the file is not found.
  2. require: It includes the specified file and stops executing the script if the file is not found.


Here are the steps to include an external file in PHP:

  1. Create the external PHP file with the necessary code (e.g., external_file.php).
  2. In the PHP file where you want to include the external file, use the include or require statement followed by the filename with its path: include 'path/to/external_file.php'; or require 'path/to/external_file.php'; Note: If the file is in the same directory, you only need to provide the filename.
  3. Save and run your PHP script, and the code from the external file will be included and executed at the specified location.
Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To include a PHP file with JavaScript, you can use an AJAX request or jQuery's $.ajax() method. Here is an example of how to do it:First, make sure you have the jQuery library included in your HTML file. You can download it from the jQuery website or inclu...
To create a simple PHP script, you'll need to follow a few steps:Open a text editor of your choice, like Notepad or Sublime Text.Start by opening PHP tags Write your PHP code within these tags. PHP code can include variables, functions, loops, conditional ...
To remove the .php extension from a URL in Nginx, you can use the following configuration in your Nginx server block: location / { try_files $uri $uri/ $uri.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var...