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.
- Python - Call function from another function
- How to call some function before main() function in C++?
- How to get the function name inside a function in PHP ?
- Difference between Virtual function and Pure virtual function in C++
- p5.js | log() function
- p5.js | cos() function
- PHP | each() Function
- p5.js | arc() Function
- p5.js | tan() function
- p5.js | sin() function
- PHP | exp() Function
- D3.js | d3.sum() function
- p5.js | max() function
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.