What is the difference between require and include in php?

青灯夜游
Release: 2023-02-28 20:20:02
Original
3493 people have browsed it

What is the difference between require and include in php?

The performance of the require() statement is similar to include(), both include and run the specified file. The difference is that with the include() statement, the file is read and evaluated each time it is executed; with require(), the file is only processed once (in effect, the file content replaces the require() statement ). This means that if the code is likely to be executed multiple times, it is more efficient to use require(). On the other hand, if you are reading a different file each time your code is executed, or if you have a loop that iterates through a set of files, use the include() statement.

require() is used as follows:

require("myfile.php")
Copy after login

This statement is usually placed at the front of the PHP script program. Before the PHP program is executed, it will first read the file introduced by the require() statement, making it a part of the PHP script file.

include() is used in the same way as require, such as:

include("myfile.php")
Copy after login

This statement is generally placed in the processing section of flow control.

The PHP script file only reads the files it includes when it reads the include() statement. In this way, the process of program execution can be simplified.

  • include Load when used

  • require Load at the beginning

  • _once suffix Indicates that the loaded one is not loaded

PHP The system has a pseudo-compilation process when loading a PHP program, which can speed up the running speed of the program. But the documentation for include is still interpreted. If there is an error in the include file, the main program will continue to execute. If there is an error in the require file, the main program will also stop. Therefore, if the error in the included file has little impact on the system (such as interface files), use include, otherwise use require.

require() and include() statements are language structures, not real functions. They can be like other language structures in php. For example, echo() can use echo("ab") form, or you can use Echo "abc" format outputs the string abc. The require() and include() statements can also add parameters directly without parentheses.

include_once() and require_once() statements also include running the specified file during script execution. This behavior is similar to include() statements and require(), and can be used in the same way. The only difference is that if the code in the file is already included, it will not be included again. These two statements should be used when the same file may be included more than once during script execution to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment.

For more related knowledge, please pay attention to PHP Chinese website! !

The above is the detailed content of What is the difference between require and include in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template