The difference between include and require

Source: Internet
Author: User

1, require is loaded before the program interpretation is executed, the content that is loaded is used frequently during the execution of the program interpretation, and the include is used in the interpretation of the execution of certain content to use, include load; If each execution code reads a different file or has a loop that iterates through a set of files, the include is used.

2,include the introduction of file failure, warning, the program continues to execute, require introduced file error, errors, stop execution.

3,include_once and require_once are similar to include and require, but the files they introduce can only be referenced once.

4,include is similar to file_get_contents, but the include return string can be executed, file_get_contents the string will not be executed.

Instance

(1) include conditional reference; require is an unconditional reference.

if ($some) {include ' somefile.php ';}

As above code. If the variable $somg is true, the file somefile.php will be included.

if ($something) {require ' somefile.php ';}

As in the code, but no matter what value $some takes, the following code will include the file somefile.php into the file.

(2) When referencing different files during the iteration

$i = 1;while ($i < 3) {require "Somefile". $i. ".  PHP "; $i + +;}

Can be seen from the above code, each time the loop, The program will include the same file somefile1.php, it is clear that this is not what we want, we can see that this code wants to be in each loop, the different files are included, if you want to complete this function, you can only use the include.

$i = 1;while ($i < 3) {include "somefile. $i. php"; $i + +;}

  

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.