Xin Xing thoroughly analyzes the differences between include and require in PHP. Xin Xing require

Source: Internet
Author: User

Xin Xing thoroughly analyzes the differences between include and require in PHP. Xin Xing require

First, let's talk about require. We know that if the file it introduces does not exist, it will cause the program to fail to be executed. Therefore, it is usually placed at the beginning of the program, which is usually a particularly important part, for example, you can connect to a database, such as loading configuration files, or referencing a core function library.

Then let's talk about the include operation. It doesn't matter whether it is introduced successfully or not. Therefore, it is very similar to html and can be used at the beginning of the program, of course, most of the time we need to include it When referencing it. In this way, its location is more casual and can be placed in the middle.

There is another include_once, which has much higher overhead than include. Why? Because it checks whether the file has been included, if it has already been introduced, it will not be re-introduced, if not, it will be included. Its usage is the same as that of include, but it will not be imported twice. There is also a require_once, which is also used to only request once, the use is the same as require.

Why cannot I import some files multiple times? The reason is very simple. For example, some files have class definitions. If we import them multiple times, it will inevitably lead to repeated class definitions, which will trigger errors, so we should avoid them.




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.