Table of contents
1.
Introduction
2.
PHP require() Function
2.1.
Syntax
2.2.
Example
2.3.
Advantages of require() in PHP
3.
PHP include() Function
3.1.
Syntax
3.2.
Example
3.3.
Advantages of include() in PHP
4.
Difference between require() and include() Functions
5.
Frequently Asked Questions
5.1.
What happens if the file specified in require() is not found?
5.2.
Can I use variables in the file path for include() and require()?
5.3.
Is it possible to include the same file multiple times using include() or require()?
6.
Conclusion
Last Updated: Oct 14, 2024
Easy

Require and Include in PHP

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

PHP is a popular scripting language which is used for web development. It provides many functions to include & execute external PHP files within a script. Out of these functions, two commonly used functions for this purpose are require() & include(). While both functions solve similar problems, there are distinct differences between them that developers should understand. 

Require and Include in PHP

In this article, we will discuss the require() & include() functions in PHP in detail, their syntax, examples, & the important differences between them.

PHP require() Function

The require() function in PHP is used to include & execute an external PHP file within the current script. When it is used, it halts the execution of the current script & includes the specified file. If the file is not found or contains errors, the script will terminate with a fatal error.

The require() function is typically used when the included file is essential for the proper functioning of the script. It ensures that the file is included only once, even if the require() statement is called multiple times.

Syntax

The syntax for the require() function is :

require 'filename.php';


Here, 'filename.php' represents the path to the PHP file you want to include. The path can be absolute or relative to the current script's directory.

Example

Let's look at an example to understand how the require() function works. Suppose we have two files: main.php & header.php.

main.php:

<?php
require 'header.php';
echo "<h1>Welcome to my website!</h1>";
?>

 

header.php:

<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>


In this example, the main.php script uses the require() function to include the header.php file. When main.php is executed, it will first include the contents of header.php, which contains the HTML structure for the page header. Then, it will continue executing the remaining code in main.php, displaying the "Welcome to my website!" heading.

Advantages of require() in PHP

1. Code reusability: By using require(), you can separate common code into separate files & include them in multiple scripts, promoting code reusability & maintainability.
 

2. Ensuring file inclusion: require() ensures that the specified file is included & executed, making it suitable for including files that are critical to the script's functionality.
 

3. Error handling: If the file specified in require() is not found or contains errors, the script will terminate with a fatal error, helping you identify & fix issues quickly.
 

4. Performance: require() is slightly faster than include() because it doesn't continue executing the script if the file is not found or contains errors.
 

5. Code organization: By using require(), you can organize your code into logical & manageable parts, making it easier to understand & maintain.
 

6. Scope management: Variables & functions defined in the required file become available in the script where it is included, allowing you to share data & functionality between files.
 

7. Consistent behavior: require() ensures consistent behavior across different scripts by including the same file & executing its code in the same way each time it is called.

PHP include() Function

The include() function in PHP works like the require() function because it lets you add and run an external PHP file within your current script. But, there are some important differences in how include() deals with errors and how it adds the files.

When the include() function is encountered, it includes the specified file & continues executing the script even if the file is not found or contains errors. If an error occurs, a warning is generated, but the script continues running.

The include() function is typically used when the included file is not critical to the script's functionality, & you want to allow the script to continue executing even if the file is missing or contains errors.

Syntax

The syntax for the include() function is :

include 'filename.php';


Again, 'filename.php' represents the path to the PHP file you want to include, which can be absolute or relative to the current script's directory.

Example

Now, let's take an example to show the use of the include() function. Suppose we have two files: main.php & footer.php.

main.php:

<?php
echo "<h1>Welcome to my website!</h1>";
include 'footer.php';
?>

 

footer.php:

<footer>
    <p>&copy; 2023 My Website. All rights reserved.</p>
</footer>
</body>
</html>


In this example, the main.php script displays the "Welcome to my website!" heading & then uses the include() function to include the footer.php file. The footer.php file contains the HTML structure for the page footer.

If the footer.php file is not found or contains errors, a warning will be generated, but the script will continue executing, displaying the heading from main.php. This behavior allows for more flexibility in handling non-critical file inclusions.

Advantages of include() in PHP

1. Flexibility: include() allows the script to continue executing even if the included file is not found or contains errors, providing more flexibility in handling non-critical file inclusions.
 

2. Error handling: If the file specified in include() is not found or contains errors, a warning is generated, but the script continues running, allowing you to handle errors more gracefully.
 

3. Code organization: Like require(), include() helps in organizing code into separate files, making it easier to manage and maintain larger projects.
 

4. Code reusability: include() promotes code reusability by allowing you to include common code snippets in multiple scripts, reducing duplication and improving maintainability.
 

5. Dynamic file inclusion: include() can be used with variable file names, enabling dynamic file inclusion based on runtime conditions or user input.

 

6. Performance: While include() is slightly slower than require() due to its error handling behavior, it still provides a way to include external files efficiently.
 

7. Conditional inclusion: include() can be combined with conditional statements to include files based on specific conditions, providing more control over the inclusion process.

Difference between require() and include() Functions

Parametersrequire()include()
File inclusionIncludes and executes the specified fileIncludes and executes the specified file
Error handlingTerminates the script with a fatal error if the file is not found or contains errorsGenerates a warning if the file is not found or contains errors, but continues executing the script
Execution behaviorHalts the script execution if an error occurs in the included fileAllows the script to continue executing even if an error occurs in the included file
Use caseUsed for including critical files that are necessary for the script's functionalityUsed for including non-critical files that are not essential for the script's functionality
PerformanceSlightly faster than include() because it doesn't continue executing the script if an error occursSlightly slower than require() because it continues executing the script even if an error occurs
Code organizationHelps in organizing code into separate files for better maintainability and reusabilityHelps in organizing code into separate files for better maintainability and reusability
ScopeVariables and functions defined in the required file become available in the script where it is includedVariables and functions defined in the included file become available in the script where it is included

Frequently Asked Questions

What happens if the file specified in require() is not found?

If the file specified in require() is not found, the script will terminate with a fatal error, and the rest of the script will not be executed.

Can I use variables in the file path for include() and require()?

Yes, you can use variables to specify the file path in both include() and require() functions, allowing for dynamic file inclusion based on runtime conditions or user input.

Is it possible to include the same file multiple times using include() or require()?

Yes, it is possible to include the same file multiple times using include(). However, with require(), the file will be included only once, even if the require() statement is called multiple times.

Conclusion

In this article, we have learned about the require() and include() functions in PHP, which are used to include and execute external PHP files within a script. We discussed their syntax, examples, and the important differences between them. require() is used for including critical files and terminates the script if an error occurs, while include() is used for including non-critical files and continues executing the script even if an error occurs. Both functions help in organizing code, promoting reusability, and improving maintainability.
You can also check out our other blogs on Code360.

Live masterclass