Change language

Why is the require_once () function so poorly used in PHP?

Syntax:
require_once (’File name with file path’); 
Parameters:This function takes one parameter "file name with path". This is the file we want to include in our PHP script. This parameter is a string type.Returned value:if the called file is found and if the file is already included, the function will return the boolean value True,and if the file is not will be enabled, the function will include the file and return True. But if the called file is not found, then a fatal error will occur, and no output is displayed, and execution will stop, returning Boolean False .The following programs illustrate the require_once() function in PHP:File name: my_file.inc.php Program :  
// File content PHP echo "Welcome To GeeksforGeeks" ; ?>
The above my_file.inc.php is included twice in the function require_once() in the following index.php file. But from the output you will get the second instance of the include being ignored because the require_once() function ignores any such include after the first.Filename: index.php  
 
// Include file require_once ( ’my_file.inc.php’ ); require_once ( ’my_file.inc. php’ );  
?>
Output:
Welcome To GeeksforGeeks 
Why is the require_once() function so bad to use?
  • The require_once() function puts a heavy load on the server , including all files.
  • The require_once() functionality does not work correctly when used inside a repeating function to store variables.
Filename: my_file.php
Example :  
 
// File content $var = ’GFG’ ;  
?>
File name: check.php function func() { require_once ( ’my_file.php’ ); return $var ; } for ( $i = 1; $i < = 3; $i ++) { echo func(). "
"
; }  
?>
Output:
GFG 
By replacing the require_once() function with the require() function in the above example, we can ensure that the $var variable is available every time the function is called. File name: check2.php function func() { require ( ’my_file.php’ ); return $var ; } for ( $i = 1; $i < = 3; $i ++) { echo func(). "
"
; }  
?>
Output:
GFG GFG GFG 
The require_once() function is slower than the require() or include() function because it checks if the file is included or not every time the script calls the function.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method