Comparison between include () and require () in php

Source: Internet
Author: User
Php's require () and include () have no major differences in performance between php's require () and include (), but have no major differences in performance.

The only difference is:

When executing include (), the file must be read and evaluated each time;
When require () is executed, the file is processed only once (in fact, the file content replaces the require () statement ).
That is, if the code that contains one of these commands and the code that may be executed multiple times, the use of require () is more efficient.

In addition, if you want to read different files each time you run the code, or there is a loop through a set of file stacks, you should use include (), because you can set a variable for the file name you want to include, this variable is used when the parameter is include.

Although php's require () performance is similar to include (), the following six differences are found through learning and searching.

1,
The difference is that for include (), the file should be read and evaluated every time when include () is executed;
For require (), the file is only processed once (in fact, the file content replaces the require () statement ).
This means that if the code containing one of these commands and the code that may be executed multiple times, the use of require () is more efficient.

2,
Require is executed only once. no, this is not appropriate. It should be said that require should be replaced first, and the content of the specified file should be replaced before running, so it does not know whether you have set the FOR loop. The include statement,

When is it executed, and when is the content of the specified file taken in.
Therefore, if you want to read different files every time you execute code, or use a set of file stacks for loops, you can use include (), because you can set a variable for the file name you want to include when the parameter is include ()

Use this variable.

3,
During the execution of include, if an error occurs in the file included in the include statement, it will not be immediately stopped; while require will immediately terminate the program and stop executing it.

4. include can be used in loops; require cannot.

5. include has a return value, but require does not (probably because of this, require is faster than include)

The code in OK. php is echo "OK! ";

$ Login = include ('OK. php ');
If (! Empty ($ login) {echo "file inclusion successful ";
} Else {echo "file inclusion failed ";}

The returned result is OK! File inclusion successful

If a statement exists in OK. php, a success is returned.

For example:

1. the code in php is as follows:
Return array (
'Loveveyou' => 1, 2, 3, 4
);
?>

The code in index. php is as follows:
$ A = array_change_key_case (include '1. php ');

Print_r ($ );
?>

The result of accessing index. php is as follows:
Array ([iloveyou] => 1 [0] => 2 [1] => 3 [2] => 4)

6. Use of require: This function is usually placed at the beginning of the PHP program. before executing the PHP program, it will first read the file specified by require, make it a part of the PHP web page. Regular

You can also use this method to introduce the function to the web page.
Include usage: This function is generally placed in the process control section. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution.

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.