How failures in execution are handled with include() and require() functions in PHP ?
Last Updated :
06 Apr, 2023
The include() and require() functions use the code or text present in the specified file and copy the code/text into the file which uses the include/require functions. The benefit of using these functions is that it saves time and lines of code because it helps us to reuse the functionalities that are already defined wherever we want by a simple single-line statement. The entire functionality of include() and require() methods are the same. But they behave differently when an execution failure happens.
include() function: It allows developers to reuse the existing functionalities with less code. The function produces a warning when any included file is missing and the script will continue to run to provide an output. So whenever we want a script to run and show users the output even when the files specified in the include statement are missing, then the include() function can be used.
Example 1: This sample example is using the include() function in PHP. The below code is the content for the "footer.php" file.
Filename: footer.php
php
<?php
echo "
<p>GeekesforGeeks.org (Learning Portal) </p>
";
?>
Include this "footer.php" file into another PHP "main.php" file.
main.php: In the below code, the include() function is used to include the "footer.php" file. The functionality of the "footer.php" file can be used in the "main.php" code. Even if the "footer.php" file does not exist, the HTML file will continue to run and provides output.
Filename: main.php
php
<!DOCTYPE html>
<html>
<head>
<title>Sample GFG</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<p>Default code has been loaded into the Editor.</p>
<?php include 'footer.php';?>
</body>
</html>
Output:
Output of Main.phpExample 2: If we try to include the non-existing PHP file into "main.php" using the include() method, we will still get an output with a warning message for the include statement. Let's look into the code that includes a not existing PHP file.
PHP
<!DOCTYPE html>
<html>
<head>
<title>Sample GFG</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<p>Default code has been loaded into the Editor.</p>
<?php include 'noFile.php';?>
</body>
</html>
Output:
Warning message in include() functionrequire(): The require() function performs same as the include() function. But it produces an error when the file specified in require function is missing. The require() function will produce a fatal error when any file specified in require function is missing and stops the script from running. This function is used in complex PHP applications that avoid compromising application security and integrity if any missing files are included.
Example: This sample example is using the require() function in PHP.
header.php: The below code is the content for the "header.php" file.
Filename: header.php
php
<?php
echo "<a>Articles</a> <a>Jobs</a> <a>Courses</a>"
?>
demo.php: Try to include this "header.php" file into another PHP "demo.php" file, will see the contents of both the files.
Filename: demo.php
php
<!DOCTYPE html>
<html>
<head>
<title>Sample GFG</title>
</head>
<body>
<?php require 'header.php';?>
<h2>Welcome To GFG</h2>
<p>Default code has been loaded into the Editor.</p>
</body>
</html>
Output:
Output of Demo.phpdemo.php: If we try to include the file which does not exist using the require() function, then a fatal error is produced and the script stops running.
Filename: demo.php
php
<!DOCTYPE html>
<html>
<head>
<title>Sample GFG</title>
</head>
<body>
<?php require 'noHeader.php';?>
<h2>Welcome To GFG</h2>
<p>Default code has been loaded into the Editor.</p>
</body>
</html>
Output:
Error message in require() functionNote: Unlike include() function, require() function will not proceed script to run, if file specified in the require() function does not exist. It throws a fatal error message.
Similar Reads
Difference between require() and include() in PHP In this article, we will see the require() and include() Functions in PHP, along with understanding their basic implementation through the illustration. Both require() and include() Functions are utilized to add the external PHP files in the current PHP script. With this, both functions enhance the
3 min read
Difference between require() and require_once() in PHP PHP require() Function: The require() function in PHP is mostly used to include the code/data of one PHP file to another file. During this process, if there are any kind of errors then this require() function will display a warning along with a fatal error which will immediately stop the execution o
2 min read
How to declare a function that receives one parameter in PHP ? In this article, we will know how to declare the function that receives the single parameter in PHP. Function calling is an important aspect of any programming language. Functions(methods) can be called within the script to simplify the performance of operations and eliminate the redundancy of writi
3 min read
PHP | include_once() and require_once() We already have learnt about file inclusion in PHP in the article PHP | (Include and Require). We have discussed about include() and require() functions for file inclusion in our previous article. In this article we will discuss about two more yet useful functions in PHP for file inclusion: include_
3 min read
How to include content of a PHP file into another PHP file ? Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include
2 min read
How to handle exceptions in PHP ? Exceptions in PHP: The exception is the one that describes the error or unexpected behavior of the PHP script. The exception is thrown in many PHP tasks and classes. User-defined tasks and classes can also do differently. The exception is a good way to stop work when it comes to data that it can use
2 min read
Describe PHP Include and Require In this article, we will see what include() & the require() functions is, also will know how these functions affect the execution of the code, their differences & usage in PHP, along with understanding their implementation through the examples. As we know PHP allows us to create various func
3 min read
Difference between include() and include_once() in PHP The include() function in PHP is mostly used to include the code/data of one PHP file to another file. During this process if there are any kind of errors then this require() function will display/give a warning but unlike the require() function in which the execution comes to a halt, the include()
3 min read
PHP get_included_files() Function The get_included_files() is an inbuilt function that returns an array with names of included or required files. Syntax: get_included_files(): array Parameters: This function does not accept any parameters. Return Values: It returns an array of the names of files. Example 1:Â This example demonstrate
1 min read
Difference between runtime exception and compile time exception in PHP The term PHP is an acronym for Hypertext Preprocessor, which is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension â.phpâ. It is an interpreted lang
3 min read