PHP Require & PHP require_Once


This post will look at some of the most valuable and significant PHP functions for file inclusion. All of these functions, including require, require_once, include, and include_once, is used to include files in a PHP page, although they differ slightly in functionality.

Let's have a look at these functions and how they work.

include() is a function that allows you to include something in

In a PHP page, this function is used to include a file. If the include() method is unable to locate a specified file on the provided location at that moment, a warning message will be displayed; nevertheless, script execution will not be halted.

require():

In a PHP page, this function is used to add a file. If the need() method cannot identify a given file, a fatal error will be generated, and the content execution will be halted.

include once():

This method is used only to add a single file at a time. If we use include once, if the code from a file has previously been included, it will not be included again (). It will create a warning message if it cannot locate a given file at that moment, but it will not stop the content execution.

require_once():

If we use require once, the code from a PHP file will not be included again if it has already been included (). It means that require once() will only add the file once. If it cannot identify a particular file, a fatal error will be generated, but the content execution will be halted.

We learnt how to use include(), require(), include once(), and require_once() in this post ().

In PHP, the require_once statement works similarly to the require statement. The only difference is that if a file has already been processed, it will not be added again. Even if the require_once statement is used, a file included with either the include or require statement will not be included again.

The require_once statement behaves similarly to the require statement in other ways.

require_once Example

The test.php file is included in the main PHP script in the Example below.

Example

<?php echo "inside main script\n"; echo "now including with require_once test.php script\n"; require_once "test.php"; echo "try to include it again"; require_once "test.php"; ?> //test.php <?php echo "File included\n"; ?>

Output

When the main script is executed from the command line, the following will happen: include the require_once test.php script file inside the main script and try to include it again.

When attempting to add a non-existing file, the error for failing require_once generates a catastrophic error.

<?php echo "inside main script\n"; echo "now including with require_once notest.php script\n"; require_once "notest.php"; ?>

Output

As a consequence, you'll get the following outcome. Note that the application has been stopped due to an error.

inside main script now including with require_once notest.php script PHP Fatal error: require_once(): Failed opening required 'notest.php' (include_path='C:\xampp\php\PEAR') in line 4 Fatal error: require_once(): Failed opening required 'notest.php' (include_path='C:\xampp\php\PEAR') in line 4