The comparison _php base of include () and require () in PHP

Source: Internet
Author: User

PHP's require () and include () are not significantly different in performance.

The only difference is that:

Include () The file must be read and evaluated each time;
Require () The file is processed only once (in fact, the contents of the file replace the require () statement).
That is, using require () is more efficient if you have code that contains one of these directives and code that can be executed more than once.

In addition, you should use include () if you want to read different files each time you execute your code, or if you have a loop that iterates through a set of files, because you can set a variable for the file name you want to include, and use that variable when the parameter is included ().

PHP's require () performance and include () although there are similar places, but through their own learning and search, or found 6 points of distinction as follows.

1,
The difference is that, for include (), files are read and evaluated every time the include () is executed;
for require (), the file is processed only once (in fact, the contents of the file replace the require () statement).
This means that if you have code that contains one of these directives and code that is likely to execute multiple times, then using require () is a high efficiency.

2,
Require is only executed once, no, that's not appropriate. It should be said that require is the first alternative, the contents of the specified file to be in, and then run, so it does not know whether you set up a for loop. And the include statement,

When it was executed, and what brought the contents of the specified file in, continued execution.
So, if you want to read a different file each time you execute your code, or if you have a loop that iterates through a set of files, use include (), because you can set a variable for the file name you want to include, when the parameter is include ()

When you use this variable.

3,
Include in the execution, if the include in the file error, will not immediately stop, and require will immediately terminate the program, no longer down execution.

4, include can be used in the cycle; require.

5, include return value, and require not (possibly because so require faster than include)

The code in the ok.php is the echo "ok!";

$login = include (' ok.php ');
if (!empty ($login)) {echo "file contains success";
}else{echo "file contains failed";}

The final return result is: ok! file contains success

As long as there is a statement in the ok.php, it will return success.

Give an example:

The code in 1.php is as follows:
<?php
Return Array (
' ILOVEYOU ' =>1,2,3,4
);
?>

The code in the index.php is as follows:
<?php
$a = array_change_key_case (include ' 1.php ');

Print_r ($a);
?>

The results of the access index.php are as follows:
Array ([iloveyou] => 1 [0] => 2 [1] => 3 [2] => 4)

6, the use of require: This function is usually placed at the top of the PHP program, PHP program before executing, will first read into the require specified the introduction of the file, so that it becomes part of the PHP Program Web page. Often

function, which can also be introduced into the Web page in this way.
Include use method: This function is generally placed in the processing part 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 a program

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.