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.

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.