What is the difference between include and require?

Source: Internet
Author: User

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, 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 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:

Warning:include (test-nothing.php) [function.include]: failed to open stream:no such file or directory in D:\www\test-inc Lude.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 on 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 $some value, 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 + +;

}

As can be seen from the above code, each time the loop, the program will be the same file included, it is clear that this is not what we want, we can see that the code wants to be in each loop, the different files included, if you want to complete this function, 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 ().

What is the difference between include and require?

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.