Difference Between Include and require and include_once

Source: Internet
Author: User
Include () and require () statements include and run the specified file. The two structures are exactly the same on the contained files, and the only difference is the handling of errors. When the require () statement encounters an error or the contained file does not exist, it stops and returns an error. Include () indicates that the row is continued. ① Functions and usage

This reduces code duplication.

Include (_ once) ("file path") and require (_ once) ("file path ")

② Understanding

To put it bluntly, replace include (_ once) and require (_ once) with the content in the contained file.

③ Note

Include/require files must be added Because when the file is included, we first understand that the content of the file is a common string. Label, to explain

④ Path

You can use an absolute path or a relative path. you can use both forward and backward slashes in windows. in linux, you can only recognize forward slashes. Therefore, it is best to use forward slashes.

⑤ Difference

Include indicates the inclusion. if the file cannot be found, the system reports a warning error and the program continues to execute.

Require is required. if the file cannot be found, a fatal error (fatal error) is reported, and the program stops executing.

After the value is added once, the system determines that if the value already exists, it will not include the second time.

Eg: there is a. php file whose content is  

The content in the B. php file is $ a = 5; require_once ("a. php"); echo $ a; require_once ("a. php"); echo $;

The first output is 6, and the second output is 6 .. _ Once is only included once. if once is not added, the second output will be 7

⑥ Trade-off

For example, if the system configuration is missing and the website does not run, require is used. if a statistical program is missing, the website only needs to count fewer people. this is not necessary, you can use include

However, adding or not adding once is the difference in efficiency. In addition, although the system helps you consider loading only once, the system's judgment will reduce the efficiency. Therefore, it should be at the beginning of development, modify the directory structure and try not to use _ once.

7. Special usage

Use include/require to return the returned values of included pages

A. php page:... return $ value; B. php page: $ v = include ("a. php ");
 

This usage is occasionally encountered during Website configuration!

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.