The require_once() function is an inbuilt function in PHP which is useful in case if we want to include a PHP file in another one i.e. when we need to include a file more than once in PHP script. It is used to check if the file is included more than once or not as it ignores all the inclusions while running the script if the file is already included.
require_once('File name with file path');
Parameters: This function accepts a single parameter ‘file name with path’. This is the file that we want to include in our PHP script. It is a string type parameter.
Return Value: If the called file is found and further if the file is already included, then the function would return the Boolean True and if the file is not included, then the function would include the file and return True. But if the called file is not found, then a fatal error will arise and no output will display and the execution halts returning Boolean False.
Below programs illustrate the require_once() function in PHP:
File Name: my_file.inc.php
The above file my_file.inc.php included twice with require_once() function in the following file index.php. But from the output, you will get the second instance of inclusion ignored since require_once() function ignores all the similar inclusions after the first one.
File Name: index.php
Welcome To GeeksforGeeks
Why require_once() function is so bad to use?
- The require_once() function puts a lot of load on the server while including all the files.
- The functionality of require_once() function works inappropriately when used inside repetitive function when storing variables.
File Name: my_file.php
File Name: check.php
By replacing the require_once() function with the require() function in the above example, we can make sure the variable
$var is available at each function call.
File Name: check2.php
GFG GFG GFG
The require_once() function is slower in comparison to require() or include() function as it checks if the file is already included or not every time the script calls the function.
- Why import star in Python is a bad idea
- Why use Guzzle Instead of cURL in PHP ?
- What is PHP and Why we use it ?
- What is the best way to include CSS file? Why use @import?
- Why to use .html instead of .htm extension?
- Why do we use $rootScope.$broadcast in AngularJS?
- Why card images use data-src (not src) for image in Bootstrap 4 ?
- Why use Question mark in TypeScript variable ?
- What is Web API and why we use it ?
- Why to check both isset() and !empty() function in PHP ?
- How to use php serialize() and unserialize() Function
- Why is === faster than == in PHP ?
- Why PHP script does not create a directory with 777 permissions ?
- Why does PHP 5.2+ disallow abstract static class methods ?
- What is the use of the @ symbol in PHP?
- How to use a switch case 'or' in PHP?
- What is the use of “=>” symbol in PHP ?
- When to use static vs instantiated classes in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.