Differences between PHP include () and require () Methods

Source: Internet
Author: User

PHP include () and require () are two methods that contain external files. Many beginners may not understand the differences between the two methods. The following summarizes the differences between PHP include () and require:

1: loading failure is handled in different ways:

Include () generates a warning, while require () causes a fatal error (an error occurs and the script stops executing)

Require (): If the file does not exist, a fatal error is reported. The script is stopped.

Include (): If the file does not exist, a warning is provided, but the script continues to run.

Note: When the include () file does not exist, the script continues to run. This occurs only before PHP 4.3.5.

We recommend that you use require_once () and include_once () to check whether the file contains duplicates.

2. php Performance

For include (), the file should be read and evaluated every time when include () is executed;

For require (), the file is only processed once (in fact, the file content replaces the require () Statement ).

This means that if the code containing one of these commands and the code that may be executed multiple times, the use of require () is more efficient.

On the other hand, if different files are read each time the code is executed, or there is a loop through a set of file stacks, use include (),

Because you can set a variable for the file name you want to include, this variable is used when the parameter is include.

3. Two methods provide different use elasticity.

The use of require is as follows: require ("./inc. php ");. Usually put at the beginning of the PHP program. Before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage.

Include usage methods such as include ("./inc/. php ");. It is generally placed in the Process Section of process control. The include file is read on the PHP webpage. In this way, you can simplify the process during program execution.

Require will be included even when the condition bit is FALSE, and include will be executed only when the change position is executed.

The require_once () statement includes 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. The functions of the require_once () function are almost the same as those of require ().

The include_once () statement includes 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. The role of the include_once () function is almost the same as that of the include () function.

The function of require_once is to check whether the file has been loaded before. If the file has not been loaded, it will not be loaded again if it has been loaded, for example, a file defines a type. If the file is loaded twice, an error occurs.

The above is the difference between the PHP include () and require () methods.


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.