Renqi in PHP include vs. require

Source: Internet
Author: User
PHP's require () and include () are not very different in terms of performance.
There are only a few differences:
The include () file is read and evaluated each time;
Require () executes the file only once (in effect, the file contents replace the Require () statement).
That is, if you have code that contains one of these directives and code that might execute multiple times, then using require () is a high efficiency.
In addition, if you want to read different files each time you execute the code, or have loops that pass through a set of file iterations, you should use include (), because you can set a variable for the file name you want to include, and use this variable when the parameter is include ().
PHP require () performance and include () although there are similar places, but through their own study and find, still found the 6 point difference is as follows.
1.
The difference is that, for the include (), the file is read and evaluated every time the include () is executed;
for require (), the file is processed only once (in fact, the file contents replace the Require () statement).
This means that if you have code that contains one of these directives and code that might execute multiple times, using require () is a high efficiency.
2.
Require is performed only once, no, that's not appropriate. It should be said that require is the first substitution, the contents of the specified file in the generation, and then run, so it does not know whether you set up a for loop. And the include statement,
When did it happen, and what brought the contents of the specified file in, and continued execution.
So, if you want to read a different file each time you execute the code, or have a loop that passes through a set of file iterations, use include (), because you can set a variable for the file name you want to include, when the parameter is include ()
When using this variable.
3.
The include does not stop immediately if the include file is in error, and require terminates the program immediately and does not go down.
4, include can be used in circulation; require.
5, include has a return value, and require not (probably because this require faster than include)
The code in ok.php is echo "ok!";
$login = include (' ok.php ');
if (!empty ($login)) {echo "file contains success";
}else{echo "file contains failed";}
The result returned is: ok! file contains success
As long as a statement exists in the ok.php, it returns to success.
In an example:
The code in 1.php is as follows:
Return Array (
' ILOVEYOU ' =>1,2,3,4
);
?>
The code in index.php is as follows:
$a = array_change_key_case (include ' 1.php ');
Print_r ($a);
?>
The results of accessing index.php are as follows:
Array ([iloveyou] = 1 [0] = 2 [1] = 3 [2] = 4)
6, the use of require: This function is usually placed in the front of the PHP program, the PHP program before execution, will be read into the require specified by the introduction of the file, so that it becomes a part of the PHP program page. Often
This method can be used to introduce it to a Web page.
Include usage: This function is generally placed in the Process Control section. The PHP Program page reads the include file before it is read in. This way, you can simplify the process of executing the program

The above describes the Renqi PHP include and require, including the renqi aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.