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.
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.
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.
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.
The test.php file is included in the main PHP script in the Example below.
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.
As a consequence, you'll get the following outcome. Note that the application has been stopped due to an error.
|