- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
Difference Between include() and require() in PHP
This article is created to differentiate between the include() and require() functions of PHP.
The include() and require() functions are used when we need to execute some script from an external file. The only difference is that include() lets the script continue its execution, whereas require() does not, if something goes wrong while executing the script of an external file.
PHP include() vs. require() example
Both include() and require() functions are the same, except that:
- The include() function produces a warning message (E_WARNING) upon failure, and the script continues its execution.
- The require() function produces a fatal error (E_COMPILE_ERROR) upon failure, to halt the execution.
Here is an example using the include() function:
<?php error_reporting(0); echo "Before include()<BR>"; include("unknown.php"); echo "After include()<BR>"; ?>
The output is:
Before include() After include()
The error_reporting(0) function call in this PHP code disables error reporting. The phrase "Before include()" is then printed to the webpage. include() is used to include the contents of another file into the current file.
In this example, "unknown.php" is being included, which is likely not present in the same directory as the current file. If the file already exists, its contents are appended to the current file.
A warning message will be generated because the file "unknown.php" does not exist. However, the user will not see this warning because error reporting has been disabled. Instead, the code will bypass the include() function call and output "After include()<BR>" to the webpage.
Here is an example using the require() function:
<?php error_reporting(0); echo "Before require()<BR>"; require("unknown.php"); echo "After require()<BR>"; ?>
The output is:
Before require()
This PHP code is similar to the previous example, but utilizes the require() function rather than include(). The function require() is used to include a file within the current file. Unlike include(), however, if the required file does not exist, a fatal error will occur and script execution will stop.
Note: The error_reporting(0) statement is used to turn off error reporting.
« Previous Tutorial Next Tutorial »