In this tutorial, you will learn the required and included functions of PHP with these function definitions, syntax, parameters, differences, and examples.
Include() and Require() Function in PHP
Here are:
Include PHP Function
The include() function is used to include a file in a PHP script. This function includes the specified file and executes its code within the current script. If the specified file is not found, a warning message is displayed, but the script continues to execute.
Note:- The PHP include() function, it will produce a warning error, without halt the execution of PHP script.
Syntax:-
Here is the syntax for the include() function:
include 'path/filename.php';
Example of include() function
The example of PHP include() function is the following:
<html> <head> //include file using include() function <?php include 'path/head.php';?> </head> <body> <h1>include() function Example</h1> </body> </html>
Require PHP Function
The require() function is similar to the include() function in that it is used to include a file in a PHP script. However, there is one important difference between the two functions. If the specified file is not found, the require() function will produce a fatal error and stop the script from executing.
Note:- The PHP require() function, it will produce a fatal error and halt the execution of php script.
Syntax:-
Here is the syntax for the require() function:
require 'path/filename.php';
Example of Require() function
The example of PHP require() function is the following:
<html> <head> //require file using require() function <?php require 'path/head.php';?> </head> <body> <h1>include() function Example</h1> </body> </html>
Example 1: How to use include()
and require()
in PHP
Here is an example of how to use include()
and require()
in PHP:
// Using include() to include a non-critical file include('header.php'); echo "Welcome to my website!"; include('footer.php'); // Using require() to include a critical file require('database.php'); $db = new Database();
In the above example, header.php
and footer.php
are included using include()
, since they contain non-critical code such as HTML markup. database.php
, on the other hand, is included using require()
, since it contains critical code such as the database connection.
Question 1:- What is the difference between include and require in PHP?
The main difference between include and require is how they handle errors when the specified file is not found. The include() function will continue to execute the script even if the file is not found, whereas the require() function will produce a fatal error and stop the script from executing.
When to use include
The include() function is useful when you want to include a file in a PHP script, but you don’t want the script to stop executing if the file is not found. For example, you may want to include a header or footer file in a PHP script, but if the file is not found, you can still display the content of the page.
When to use require
The require() function is useful when you want to include a file in a PHP script, but you want the script to stop executing if the file is not found. For example, you may want to include a configuration file in a PHP script, but if the file is not found, the script cannot run properly and should stop executing.
Conclusion
the include()
and require()
functions are both useful for including PHP files into other PHP files. The key difference between them is how they handle errors when the file cannot be found or there is an error in the included code. Developers should choose the appropriate function based on the criticality of the included file to their script’s operation.
And the difference between include and require in PHP comes down to how they handle errors when the specified file is not found. The include() function will continue to execute the script, while the require() function will produce a fatal error and stop the script from executing. Use include() when you want to include a file but don’t want the script to stop executing if the file is not found. Use require() when you want to include a file, but you want the script to stop executing if the file is not found.
Recommended PHP Tutorial:
- Autocomplete Search Box in PHP MySQL
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP
- Get Highest Value in Multidimensional Array PHP
- PHP Get Min or Minimum Value in Array