What are the differences between include, require, and require_once statements? What are the differences between include, require, and require_once statements?
Reply to discussion (solution)
The include () statement contains and runs the specified file.
These two structures are identical except for how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (). This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set. Note that before PHP 4.3.5, the program will not be stopped due to a syntax error in the inclusion file, but will be executed after this version.
Include_once
The include_once () statement contains and runs The specified file during script execution. This behavior is similar to the include () statement. The only difference is that if the code in the file has been included, it will not be included again. As the statement name implies, it will only be included once.
Include_once () should be used when the same file may be contained more than once during script execution, to ensure that it is only included once to avoid function redefinition, variable re-assignment and other issues.
Require_once
The require_once () statement contains and runs the specified file during script execution. This behavior is similar to the require () statement. The only difference is that if the code in the file has been included, it will not be included again. For how this statement works, see The require () documentation.
Require_once () should be used when the same file may be contained more than once during script execution, to ensure that it is only included once to avoid function redefinition, variable re-assignment and other issues.
1. include ()
Include (/path/to/filename)
The include () statement will contain a file at the location where it is called. A file contains the same content as the data copied to the file where the statement is located.
Parentheses can be ignored when include () is used.
You can execute the include () statement according to the conditions. The use of include () in conditional statements has a strange phenomenon. it must be enclosed in statement block braces or enclosed by other statements.
2. include_once ()
Include_once (filename)
The role of the include_once () function is the same as that of the include function, but it will first verify whether the file is included. If it already exists, revoke de_once is not executed. Otherwise, the file must be included. This is the same as include.
3. require ()
Require (filename)
To a large extent, require () is the same as include. a template file is included in the location where the require call is located.
There are two important differences between require and include. First, regardless of the location of require, the file will be included in the script where require appears. For example, even if require is placed in an if statement whose calculation result is false, it still contains the specified file.
The second important difference is that when a require error occurs, the script stops running, and the script continues to run when the include is used.
4. require_once ()
Require_once (filename)
As the website grows bigger, some files may be repeatedly contained. This may not be a problem, but after modifying the variable of the included file, it is overwritten because it contains the original file again. This may not happen. Another problem may also occur, that is, the conflict of letters in the file. Use require_once to solve these problems.
The require_once function ensures that the file is only contained once. If you encounter require_once and try to include the same file later, it will be ignored.
1. include ()
Include (/path/to/filename)
The include () statement will contain a file at the location where it is called. A file contains the same content as the data copied to the file where the statement is located.
Parentheses can be ignored when include () is used.
You can execute the include () statement according to the conditions. The use of include () in conditional statements has a strange phenomenon. it must be enclosed in statement block braces or enclosed by other statements.
2. include_once ()
Include_once (filename)
The role of the include_once () function is the same as that of the include function, but it will first verify whether the file is included. If it already exists, revoke de_once is not executed. Otherwise, the file must be included. This is the same as include.
3. require ()
Require (filename)
To a large extent, require () is the same as include. a template file is included in the location where the require call is located.
There are two important differences between require and include. First, regardless of the location of require, the file will be included in the script where require appears. For example, even if require is placed in an if statement whose calculation result is false, it still contains the specified file.
The second important difference is that when a require error occurs, the script stops running, and the script continues to run when the include is used.
4. require_once ()
Require_once (filename)
As the website grows bigger, some files may be repeatedly contained. This may not be a problem, but after modifying the variable of the included file, it is overwritten because it contains the original file again. This may not happen. Another problem may also occur, that is, the conflict of letters in the file. Use require_once to solve these problems.
The require_once function ensures that the file is only contained once. If you encounter require_once and try to include the same file later, it will be ignored.
Continue upstairs:
To a large extent, require () is the same as include. a template file is included in the location where the require call is located.
There are two important differences between require and include. First, regardless of the location of require, the file will be included in the script where require appears. For example, even if require is placed in an if statement whose calculation result is false, it still contains the specified file.
The second important difference is that when a require error occurs, the script stops running, and the script continues to run when the include is used.
Frequently used interview questions. It was said upstairs.
Generally, require and require_once are used.
Simple:
All three are import files.
When the introduced file does not exist, include will have a warning, but will not stop code execution, and require will stop code execution,
Require_once only introduces the file once to prevent function and other redefinition.
1. include (): contains a file at the called location;
2. the functions of the nclude_once () function are the same as those of the include function;
3. require (): Similar to include, a template file is included in the location where require is called.
4. the require_once function ensures that the file is only contained once. If you encounter require_once and try to include the same file later, it will be ignored.
Differences:
1. when a require error occurs, the script stops running, and the include statement continues to be executed.
2. regardless of the location of require, the file will be included in the script where require appears. For example, even if require is placed in an if statement whose calculation result is false, it still contains the specified file.
3. the difference between the include_once () function and the include function: include_once () first checks whether the file is included. If it already exists, revoke de_once is not executed. Otherwise, the file must be included.