PHP contains file functions include, include_once, require, require_once difference Summary _php Tutorial

Source: Internet
Author: User
The include (), require () statement contains and runs the specified file. The two structures are exactly the same on the containing file, and the only difference is the handling of the errors. The require () statement stops when the containing file does not exist or is faulted, and an error is encountered. The include () continues.

For example, the following code:

The code is as follows: include (' hello.php ');

echo ' include Test final! '; /include error, but will continue to execute, display: include Test final!

Require (' hello.php ');

echo ' Require test final! '; /require error, stop code execution.

A sentence summary:

1.include () generates a warning

2.require () causes a fatal error

In other words, if you want to stop processing a page when you lose a file, don't hesitate to use require (). The include () is not the case and the script will continue to run. Also be sure to set the appropriate include_path.

That is, the parser will read the require file, instead of parsing, if you cannot read the Require file, you cannot proceed to the next action. Therefore, not being properly included will lead to the application of the file, with require better. may also be slightly higher in efficiency.

Note: Require () will contain files anyway, and include () can optionally contain:

The code is as follows:

if (FALSE) {

Require (' x.php ');

}

if (FALSE) {

Include (' s.php ');

}

?>

In the code above: x.php is bound to be included, and s.php must not be included.

Two ways to provide different use flexibility:

Require use methods such as require ("myrequirefile.php");. This function is usually placed at the front of the PHP program, and before the PHP program is executed, it is read into the file specified by require to make it a part of the PHP program's Web page.

Include usage methods such as include ("myincludefile.php");. This function is generally placed in the processing section of the Process Control. The PHP Program page reads the include file when it is read. This way, you can simplify the process of executing the program.

I. Use of grammar and introduction

1. Include ()

Syntax: include (/path/to/filename)

The Include () statement contains a file at the location where it was called. Contains a file that has the same content as the data in which the file was copied at the location of the statement.

Parentheses can be ignored when using include ().

The include () statement can be executed according to the criteria. Using the include () in a conditional statement has a strange phenomenon, which must be enclosed in a statement block brace, or surrounded by other statement bracketing.

2, Include_once ()

Syntax: include_once (filename)

The include_once () statement contains and runs the specified file during script execution. This behavior is similar to the include () statement, with the only difference being that include_once () will first determine if the file has been previously included, and if it is already included, ignore the inclusion.

Include_once () should be used for nested containment cases, to ensure that it is only included once to avoid problems such as function redefinition, variable re-assignment, and so on.

Summary: the include_once () function works the same as include, but it first verifies that the file is already included. If it is already included, Include_once is no longer executed. Otherwise, you must include the file. Except this is exactly the same as the include.

3, require ()

Syntax: require (filename)

Require () is, to a large extent, the same as include, where a template file is included in the location where the require call sits.

There are two important differences between require and include. First, the file will be included in the script that appears require, regardless of the location of the require. For example, even if the require is placed in an if statement that evaluates to FALSE, the specified file will still be included.

The second important difference is that when a require error occurs, the script stops running, and the script continues to execute with include.

4, require_once ()

Syntax: require_once (filename)

The require_once () statement contains and runs the specified file during script execution. This behavior is similar to the Require () statement, with the only difference being that require_once () will first determine if the file has been previously included, and if it is already included, ignore the inclusion.

Require_once () should be used for nested containment cases, to ensure that it is only included once to avoid problems such as function redefinition, variable re-assignment, and so on.

Summary: As the site grows larger, there may be duplicates containing certain files. This may not be a problem, but once you have modified the variables for the included files, you may not want this to happen because they are overwritten by the subsequent inclusion of the original file. There may also be another problem, which is the conflict of function names in the included files. Use require_once to solve these problems.

The Require_once function ensures that the file is included only once. After encountering require_once, the subsequent attempt to include the same file is ignored.

Ii. Summary of differences

1. The include () and require () statements differ.

The difference: The two structures are exactly the same except for how to handle failures.

The include () generates a warning that the script will continue to run.

Require () causes a fatal error and the script stops running.

In other words, if you want to stop processing a page when you encounter a lost file or encounter an error, use require (). Use include () if you want to continue processing the page when an error is encountered.

Note Before PHP 4.3.5, a syntax error in the include file does not cause the program to stop, but from this version.

2. The difference between include_once (), require_once () and include (), require ()

Include_once (), like require_once (), should be used to make sure it is included only once to avoid problems such as function redefinition, variable re-assignment, and so on, when the same file is likely to be included more than once during script execution. This is the main difference between include_once () and require_once () and include () and require ().

Iii. issues to be aware of

1. Path issues

Especially when nesting, be careful to include the path of the file.

For example, a file contains b files, b files contain c files, a,b,c files are not in the same folder, this time often easily error.

Solution: You can use the DirName (__file__) statement, which means to get the absolute path of the current script. such as: require_once (DirName (__file__). ' /config.php ');

2. Efficiency issues

Include_once (), require_once (), compared to include (), require (), is less efficient because they have to at least determine if the file is already contained. This issue has been greatly improved in the PHP5 version, but there is still a difference in efficiency.

http://www.bkjia.com/PHPjc/750257.html www.bkjia.com true http://www.bkjia.com/PHPjc/750257.html techarticle The include (), require () statement contains and runs the specified file. The two structures are exactly the same on the containing file, and the only difference is the handling of the errors. The Require () statement encountered in the include file does not ...

  • 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.