An explanation of the differences between include and require in PHP

Source: Internet
Author: User
Tags pear

1. Overview

of the Require () statementPerformanceWith the include () phasesimilar, areincludes and runs the specified file . The difference is that the include ()statement, when executing a fileeveryare going to beReadAndEvaluationfor require (), the fileprocess 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 use methods such as: 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 in load with
    • require in first loads
    • _once suffix denotes loaded does not load

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, do not exist a name is test-nothing.php file.

test-include.php

<?php

Include ' test-nothing.php ';

Echo ' abc ';

?>

test-require.php

<?php

Require ' test-nothing.php ';

Echo ' abc ';

?>

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

warning:include ( test-nothing.php) [function.include]: failed to open stream:no such file or directory in D:\www\test-include.php on line 2

warning : Include () [function.include]: Failed opening ' test-nothing.php ' for inclusion (include_path = '.; C:\php5\pear ') in D:\www\test-include.php to line 2

abc

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:

Warning:require (test-nothing.php) [function.require]: failed to open stream:no such file or directory in D:\www\test-req Uire.php on line 2

Fatal error:require () [function.require]: Failed opening required ' test-nothing ' (include_path= '); C:\php5\pear ') in D:\www\test-require.php on line 2

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 $somg is true, the file somefile.php will be included:

if ($some) {

include ' somefile.php ';

}

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

if ($something) {

require ' somefile.php ';

}

The following example illustrates the difference between the two functions:

$i = 1;

while ($i < 3) {

require   "Somefile. $i. php";

$i + +;

from the above code, you can see that each time the loop, the program will is included, obviously this is 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 ():

$i = 1;

while ($i < 3) {

  include "Somefile. $i. php";

$i + +;

}

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, using require () is more efficient, if you are reading a different file each time you execute the code, or you have a loop that passes through a set of file iterations, you can set a variable for the file name you want to include by using an include () This variable is used when the parameter is include ().

An explanation of 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.