PHP contains file functions include, include_once, require, require_once difference summary _php instances

Source: Internet
Author: User

For example, the following code:

Copy Code code as follows:
Include (' hello.php ');
echo ' include Test final! '; /include the error, but continues execution, showing: include Test final!
Require (' hello.php ');
echo ' Require test final! '; /require the error, stop the execution of the code.

A word summary:
1.include () generates a warning
2.require () causes a fatal error

In other words, if you want to stop processing the page when you lose the file, don't hesitate to use require (). Include () is not the case, the script will continue to run. Also, be sure to set the appropriate include_path.
That is, when the program is require to read the file, rather than parsing, if you can not read the file is require, you can not do the next move. Therefore, not to be properly included will cause the program file, with require better. may also be slightly more efficient.

Note: Require () will include files in any case, and include () can optionally include:

Copy Code code as follows:

<?php
if (FALSE) {
Require (' x.php ');
}
if (FALSE) {
Include (' s.php ');
}
?>

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

Two ways to provide different elasticity of use:
Require use methods such as require ("myrequirefile.php"); This function is usually placed at the top of the PHP program, before the PHP program is executed, it will read into the file introduced by require, making it a part of the PHP Program Web page.
Include use methods such as include ("myincludefile.php");. This function is typically placed in the processing section of the Process Control. The PHP Program page reads the included file when it is read. This way, you can simplify the process of executing your program.


I. The use of grammar and introduction

1, include ()
Syntax: include (/path/to/filename)
The Include () statement contains a file at the location where it is invoked. An effect that contains a file with the same content as the data that copies the file at the location of the statement.
Parentheses can be omitted when using include ().

The include () statement can be executed according to criteria. Using include () in a conditional statement is a strange phenomenon that must be enclosed in a statement block brace or surrounded by other statement-bounding characters.

2, Include_once ()
Syntax: include_once (filename)

The include_once () statement contains and runs the specified file during the execution of the script. This behavior is similar to the include () statement, except that include_once () first determines whether the file has been included before and, if it is already contained, ignores the inclusion.
Include_once () should be used in nested inclusions to ensure that it is included only once to avoid problems such as function redefinition, variable 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, the include_once is no longer executed. Otherwise, you must include the file. Except this is exactly the same as include.

3, require ()
Syntax: require (filename)
Require () is, to a large extent, the same as include, where a template file is included where the require call sits.
There are two important differences between require and include. First, regardless of the location of the require, the documentation will be included in the script that appears require. For example, even if require is placed in an if statement that evaluates to FALSE, the specified file is still included.
The second important difference is that the script will stop running when the require error occurs, and the script will continue to execute if the include is used.

4, require_once ()
Syntax: require_once (filename)
The require_once () statement contains and runs the specified file during the execution of the script. This behavior is similar to the Require () statement, except that require_once () first determines whether the file has previously been included, or, if it is already contained, ignores the inclusion.
Require_once () should be used in nested inclusions to ensure that it is included only once to avoid problems such as function redefinition, variable assignment, and so on.

Summary: As the site grows larger, there may be duplicates containing some files. This may not be a problem, but it may not be desirable to have a variable that contains files that are overwritten after they are again included in the original file. There may also be another problem, that is, the conflict of function names in the included file. Using require_once can solve these problems.
The Require_once function ensures that the file is included only once. After Require_once is encountered, subsequent attempts to include the same file are ignored.


Ii. Summary of differences

1, include () and require () statement difference.
The difference: The two structures are exactly the same except how to handle failures.
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 the page when you encounter a missing file or encounter an error, use require (). Use include () if you want to continue processing the page if you encounter an error.
Note that the syntax error in the include file does not cause the program to stop until PHP 4.3.5, but after this version.

2, the difference between include_once (), require_once () and include (), require ()
Include_once (), like require_once (), should be used in cases where the same file may be included more than once during the execution of a script, to ensure that it is included only once to avoid problems such as function redefinition, variable assignment, and so on. This is the main difference between include_once () and require_once () and include () and require ().


Iii. issues to be noted

1. Path issues
Especially when nesting is included, be sure to note the path of the containing file.
For example, a file contains b files, b file contains c files, a,b,c files are not in the same folder, this time is often very easy to make mistakes.
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 (), with include (), require (), less efficient, because at least they have to determine whether the file is included. The problem has been greatly improved in the PHP5 version, but there is still a difference in efficiency.

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.