The difference between PHP include_once and require_once

Source: Internet
Author: User
① Effect and usage

Can reduce duplication of code

Include (_once) (path to file) and require (_once) (Path to file)

② Understanding

To put it bluntly, it is to replace include (_once) with the contents of the included file, require (_once) line

③ Note

Include/require contains the file must be added <?php?> because in the inclusion, the first understanding of the contents of the file is a normal string, encountered <?php?> tag, only to explain

④ Path

You can use an absolute path or a relative path, you can use a forward slash in Windows, and a forward slash in Linux, so it's best to use a positive slash

⑤ differences

Include contains the meaning that when the file is not found, the warning error is reported, and then the program continues to execute

Require is a must, the file will be reported fatal error (fatal error), the program stops execution down

After adding once, the system will judge if it is already included, it will not contain the second time

Eg: there is a a.php file content of <?php $a + +; >

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

Results The first output is 6, the second output or 6, Description: _once is only included once, if not once, then the second output will be 7

⑥ Trade-offs

For example, the system configuration, missing, the site does not let the operation, natural with require, if it is a section of statistical procedures, less, the site is only a few statistics, it is not necessary, you can use include

and add once is the difference in efficiency, coupled with once, although the system to help you consider only load once, but the system will be less efficient judgment, therefore, should be at the beginning of development, the directory structure to adjust, try not to use the _once situation.

⑦ Special Usage

Returns the returned value of the contained page using Include/require

a.php page: ... return $value; b.php page: $v = include ("a.php");
<!--[endif]-->

This usage is occasionally encountered when making a 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.