Home >Backend Development >PHP Tutorial >Usage of require_once in php
require_once function is used in PHP to include files to ensure that the file is only included once to avoid repeated inclusion errors. Syntax: require_once(string $filename) Parameters: $filename, the file path to be included. Function: Include the specified file into the current script. If the file is already included, it will no longer be included. Difference: Similar to include_once, but if the file is not found, require_once generates a fatal error and interrupts execution, while include_once generates a warning.
Usage of require_once in PHP
require_once is a function in PHP used to include files, which ensures A file is only included once to avoid errors caused by including files twice.
Syntax
<code class="php">require_once(string $filename);</code>
Parameters
Parameters | Description |
---|---|
filename |
The path to the file to include. |
Function
require_once function includes the specified file into the current script. The specified filename
is included if it is not already included. If it is already included, no further inclusion is performed.
Differences from include_once
require_once and include_once functions have similar functions, but they differ in error handling:
filename
If not found, require_once will cause a fatal error and interrupt script execution. filename
is not found, include_once will generate a warning but will not interrupt script execution. Note:
Example
The following is an example of using require_once to include files:
<code class="php"><?php require_once('header.php'); // 包含头部文件 echo "页面内容"; // 执行页面内容 require_once('footer.php'); // 包含尾部文件 ?></code>
In this example, the header and tail files are only will be included once, even if they are called multiple times in the script.
The above is the detailed content of Usage of require_once in php. For more information, please follow other related articles on the PHP Chinese website!