Mastering the Difference _php skill of include_once and require_once in depth

Source: Internet
Author: User
① function and Usage

Can reduce the duplication of code

Include (_once) ("Path to File") and require (_once) ("Path to File")

② Understand

To put it bluntly, use the contents of the included file instead of include (_once), require (_once) line

③ Note   

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

④ Path

You can use an absolute path, or a relative path; windows can be a diagonal slash, Linux only forward slash, so it is best to use forward slash

⑤ Difference

Include means that when a file is not found, a warning error is reported and the program continues to execute

Require is a must mean, can not find the file, will be reported fatal error (fatal errors), the program to stop down to execute

After adding once, the system will make a judgment and if it is already included, it will not contain a second

Eg: there is a a.php file content is <?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 is 6, indicating ... _once only once, if not add once, then the second output will be 7

⑥ Choice

For example, the system configuration, the lack of, the site does not allow the operation, the natural use of require, if it is a section of the statistical procedures, less, the site is only a small number of statistics, not necessarily, you can use include

and add once is the difference in efficiency, plus once, although the system to help you consider loading only once, but the system's judgment will be inefficient, therefore, should be at the beginning of development, the directory structure to adjust well, try not to use the _once situation.

⑦ Special Usage

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

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

This usage is occasionally encountered when doing site 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.