2016/3/27 the differences between include and require in PHP

Source: Internet
Author: User
Tags pear

1. Overview

The performance of the require () statement is similar to include (), which includes and runs the specified file. The difference is that, for the include () statement, it is read and evaluated every time the file is executed, whereas for require (), the file is processed only once (in fact, the file contents replace the Require () statement). This means that the use of require () is more efficient if the code can be executed more than once. On the other hand, if you are reading a different file each time you execute the code, or you have a loop that iterates through a set of files, use the Include () statement.

Require is used in the following way: Require ("myfile.php"), this statement is usually placed at the front of the PHP script. Before the PHP program executes, it will read into the file introduced by the Require () statement, making it part of the php script file. Include uses the same way as require: include ("myfile.php"), which is typically placed in the Process Control section. The php script file is read into the include () statement before the file it contains. this way, you can simplify the process of executing the program.

    • Incluce load when used
    • Require is loaded at the beginning
    • The _once suffix indicates that the load is not loaded

The PHP system has a pseudo-compile process when loading PHP programs, which can make the program run faster. However, the Incluce document is still interpreted for execution. Include file error, the main program continues to execute, require file error, the main program also stopped, so the inclusion of file errors on the system has little impact (such as interface files) with include, otherwise with require.

The Require () and include () statements are language constructs, not real functions, and can be like other language constructs in PHP, such as Echo () using echo ("AB"), or you can use echo "ABC" to output the string ABC. Require () and include () statements can also be directly added without parentheses.

The include_once () and require_once () statements also include running the specified file during script execution. This behavior is similar to the include () statement and require (), using the same method. The only difference is that if the code in the file is already included, it will not be included again. These two statements should be used in cases where the same file may be included more than once during script execution, ensuring that it is included only once to avoid problems such as function redefinition and variable re-assignment.

2, Details 2.1 error

When the include introduces a file, if it encounters an error , it gives a hint and continues to run the code below .

Require when the file is introduced, if it encounters an error , it will give a hint and stop running the code below.

Use the example to speak, write two php files, name test-include.php and test-require.php, note the same directory, there is no file name is test-nothing.php.

1 test-include. PHP 2 3 <? PHP 4 5 include ' test-nothing.php '; 6 7 Echo ' abc '; 8 9 ?>

Browse http://localhost/test-include.php, because did not find the test-nothing.php file, we see the error message, at the same time, the error message below the display of ABC, you can see a similar situation below:

1 include (test-nothing.php) [function.  Includefile or directory in D:\www\test-include. PHP on line 223include () [function.  Include for inclusion (include_path= '.; C:\php5\pear ') in D:\www\test-include. PHP To Line 245 ABC

Use the example to speak, write two php files, name test-include.php and test-require.php, note the same directory, there is no file name is test-nothing.php.

1 test-require. PHP 2 3 <? PHP 4 5 require ' test-nothing.php '; 6 7 Echo ' abc '; 8 9 ?>

Browse http://localhost/test-require.php, because we did not find the test-nothing.php file, we see the error message, but, the error message is not shown below the ABC, you can see a similar situation below:

1 require (test-nothing.php) [function.  Requirefile or directory in D:\www\test-require. PHP To Line 223require () [function.  Require]: Failed opening required ' test-nothing ' (include_path= '); C:\php5\pear ') in D:\www\test-require

2.2 Article references

The Include () function is the same as require (), but there are some differences in usage , include () is a conditional include function, and require () is an unconditional include function.

For example, if the variable $some is true, the file somefile.php will be included:

1 if ($some) {23include ' somefile.php '; 4 5 }   

However, regardless of the $some value , the following code will include the file somefile.php into the file:

1 if ($some) {23require ' somefile.php '; 4 5 }   

The following example illustrates the difference between the two functions:

 1   $i  = 1  2   While  ( $i  < 3 4  5  require  "Somefile. $i . php ";  6   $i  ++;  8  9 } 

The

can be from above this code shows that , the program will The same file is included , which is obviously not what we want, You can see that this code wants to include different files in each loop, and if you want to do this, you can only use the function include ():

1 $i = 1; 2 3  while ($i < 3) {45include "Somefile. $i. php "; 6 7 $i+ +; 8 9 }      
2.3 File Reference Methods

Include () files that need to be referenced every time they are read and evaluated , require () will need to refer to the file to be processed only once (in fact, the file content that needs to be referenced at the time of execution is replaced by require () statement), you can see that if you have code that contains one of these directives and code that might execute multiple times , use require () is more efficient, if you read different files each time you execute the code , or you have a stack of files through a set of loop, use include () to set a variable for the file name you want to include, and use this variable when the parameter is include ().

2016/3/27 the differences between include and require in PHP

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.