Differences between include and require in PHP-php Tutorial

Source: Internet
Author: User
Differences between include and require in PHP

Recently, some friends asked questions about require_once. the boring difficulty of the problems encountered in the program is worth exploring and solving as long as the bug exists. To solve this problem, I will summarize the detailed usage of include and require. (Combined with information and understanding, integrated) hope to provide a little help to the friends in need.

Include and require:

The performance of The require () statement is similar to that of include (), which includes and runs the specified file. The difference is that for the include () statement, the file must be read and evaluated each time it is executed; For require (), the file is processed only once (in fact, replace the file content with the require () statement ). This means that if the code may be executed multiple times, the use of require () is more efficient. On the other hand, if different files are read every time the code is executed, or there is a loop through a set of file iterations, the include () statement is used.

The use of require is as follows: require ("myfile. php"). This statement is usually placed at the beginning of the PHP script program. Before the PHP program is executed, it will first read the file introduced by the require () statement to make it part of the PHP script file. The use of include is the same as that of require, such as: include ("myfile. php"). This statement is generally placed in the process section of process control. When the include () statement is read, the PHP script file reads the files it contains. In this way, you can simplify the process during program execution.

• Incluce loaded when used

• Require load at the beginning

• _ Once suffix indicates that loaded files are not loaded

The PHP System has a pseudo-compilation process when loading the PHP program, which can speed up the program running. However, the CE documentation is still interpreted. An error occurred in the include file. the main program continues to execute. The require file has an error and the main program has stopped. Therefore, if the file error does not affect the system (such as interface files) use include; otherwise, use require. The require () and include () statements are language structures and are not real functions. they can be the same as other language structures in php. for example, echo () can use echo ("AB ") format, you can also use echo "abc" to output the string abc. The require () and include () statements can also directly add parameters without parentheses.

Include_once () and require_once ():

The include_once () and require_once () statements also include running the specified file during script execution. This behavior is similar to the include () statement and require () statement, and the usage is the same. The only difference is that if the code in this file has been included, it will not be included again. These two statements should be used to ensure that the same file is only included once when it may be included more than once during script execution, to avoid function redefinition and variable re-assignment.

File reference method:

Include () the file to be referenced during execution must be read and evaluated each time. require () the file to be referenced is only processed once during execution (the require () statement replaces the file content to be referenced during actual execution) it can be seen that if there is code containing one of these commands and code that may be executed multiple times, the use of require () is more efficient, if different files are read each time code is executed or there is a loop through a set of file stacks, you can use include () to set variables for the file names you want to include, this variable is used when the parameter is include.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.