Summary of differences between include, include_once, require, and require_once file functions in PHP

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.

For example, the following code:

 

 

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

Echo 'include test final! '; // The include statement returns an error, but the execution continues. The result is: include test final!

Require ('hello. php ');

Echo 'require test final! '; // An error is reported for require to stop code execution.

 

 

Summary:

1. include () generates a warning

2. require () causes a fatal error.

 

In other words, if you want to stop processing the page when the file is lost, do not hesitate to use require. This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set.

That is to say, when parsing the program, it reads the require file instead of the file to be parsed. If you cannot read the file to be require, you cannot perform the next action. Therefore, improper inclusion may lead to program files. It is better to use require. The efficiency may also be slightly higher.

 

Note: require () will contain files in any way, and include () can selectively include:

 

The Code is as follows:

<? Php

If (FALSE ){

Require ('x. php ');

}

If (FALSE ){

Include ('s. php ');

}

?>

In the above Code: x. php will be included, but s. php will not.

 

Two methods provide different elasticity:

The use of require is as follows: require ("MyRequireFile. php ");. This function is usually placed at the beginning of the PHP program. Before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage.

Include usage methods such as include ("mydomaindefile. php ");. This function is generally placed in the Process Section of process control. The include file is read on the PHP webpage. In this way, you can simplify the process during program execution.

 

 

I. syntax and introduction

 

1. include ()

Syntax: include (/path/to/filename)

The include () Statement will contain a file at the location where it is called. A file contains the same content as the data copied to the file where the statement is located.

Parentheses can be ignored when include () is used.

 

You can execute the include () statement according to the conditions. The use of include () in conditional statements has a strange phenomenon. It must be enclosed in statement block braces or enclosed by other statements.

 

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. The only difference is that include_once () first checks whether the file has been included. if it already exists, ignore this inclusion.

Include_once () should be used for nested inclusion. To ensure that it is only included once, the function definition and variable re-assignment should be avoided.

 

Summary: The Role of the include_once () function is the same as that of include, but it will first verify whether the file is included. If it already exists, revoke de_once is not executed. Otherwise, the file must be included. This is the same as include.

 

3. require ()

Syntax: require (filename)

To a large extent, require () is the same as include. A template file is included in the location where the require call is located.

There are two important differences between require and include. First, regardless of the location of require, the file will be included in the script where require appears. For example, even if require is placed in an if statement whose calculation result is false, it still contains the specified file.

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

 

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. The only difference is that require_once () first checks whether the file has been included before. if it already exists, ignore this inclusion.

Require_once () should be used for nested inclusion, and it should be included only once to avoid the problem of function redefinition and variable re-assignment.

 

Summary: As the website grows bigger, some files may be repeatedly contained. This may not be a problem, but after modifying the variable of the included file, it is overwritten because it contains the original file again. This may not happen. Another problem may also occur, that is, the conflict of letters in the file. Use require_once to solve these problems.

The require_once function ensures that the file is only contained once. If you encounter require_once and try to include the same file later, it will be ignored.

 

 

Ii. Differences

 

1. Differences between include () and require () statements.

Difference between the two: the two structures are identical except for how to handle failures.

Include () generates a warning, and the script continues to run.

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

 

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

Note that before PHP 4.3.5, the program will not be stopped due to a syntax error in the inclusion file, but will be executed after this version.

 

2. Differences between include_once (), require_once (), include (), and require ()

Include_once () and require_once () should be used when the same file may be contained more than once during script execution, make sure it is included only once to avoid the problem of function redefinition and variable re-assignment. This is the main difference between include_once () and require_once () and include () and require.

 

 

Iii. Notes

 

1. Path Problems

In particular, you must pay attention to the path of the file to be included in nesting.

For example, if file A contains file B, and file B contains file C, A, B, and C are not in the same folder, errors may occur.

Solution: You can use the dirname (_ FILE _) statement to obtain the absolute path of the current script. For example: require_once (dirname (_ FILE _). '/config. php ');

 

2. efficiency issues

Include_once (), require_once (), compared with include (), require (), the efficiency is lower, because they must at least determine whether the file is included. This problem has been greatly improved in PHP5, but the efficiency is still different.

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.