Difference between include () and require () in PHP

Source: Internet
Author: User

There are two methods to reference files: require and include. The two methods provide different elasticity.

The use of require is as follows:require("MyRequireFile.php");. This function is usually placed 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. This method can also be used to introduce common functions into webpages.

The require file has an error and the main program has stopped.

The use of include is as follows:include("MyIncludeFile.php");. This function is generally placed in the Process of process control. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution.

If an error occurs in the include file, the main program continues to execute.

The difference between require and include is that when an error occurs, one is error and the other is warning.

Include has a return value, but require does not (probably because so require is faster than include)

$ Login = include ('test. php ');
If (! Empty ($ login )){
Echo "File Inclusion succeeded ";
} Else {
Echo "File Inclusion failed ";
}

Include is generally used to load templates, foreach loop bodies, configuration files, and cache files. Avoid fatal errors.

Require is generally used to load functions and classes.

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.