The difference between include () and require () in PHP

Source: Internet
Author: User
Tags pear

1. How to refer to a file

For include (), files are read and evaluated every time the include () is executed, and 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. On the other hand, if you read different files each time you execute the code, or if you have loops that pass through a set of file iterations, use include () because you can set a variable for the file name you want to include.

2. Is there a conditional reference

In PHP, the Include () function is the same as require (), but there are some differences in usage, include () is conditional include function, and require () is an unconditional include function. For example, in the following example, if the variable $somgthing is true, it will contain the file Somefile:

1 if ($something) {2 include ("Somefile"); 3 }

However, regardless of the $something value, the following code will include the file somefile into the file:

1 if ($something) {2 require ("Somefile"); 3 }

The following interesting example illustrates the difference between the two functions.

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

In this code, every time a loop is made, the program will include the same file. Obviously this is not the intention of the programmer, and from the code we can see that this code wants to include different files in each loop. If you want to complete this function, you must ask for the function include ():

1 $i = 1 ; 2 while ($i < 3) {3 include ("Somefile. $i"); 4 $i + +; 5 }

3. Error

Use the example to speak, write two php files, name test1.php and test2.php, note the same directory, do not exist a name is test999.php file.

test. php <? PHP include ("test999. php "); echo  "abc";?> test2. php <? PHP require ("test999. php ") echo  " abc "; ? >

Browse the first file, because we did not find the test999.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 (test1aaa.php) [function.include]: failed to open stream:no such file or directory in D:\WebSite\test.php On line 2

Warning:include () [function.include]: Failed opening ' test1aaa.php ' for inclusion (include_path= '.; C:\php5\pear ') in D:\WebSite\test.php on line 2
Abc

Browse the second file, because we did not find the test999.php file, we see the error message, but, the error message is not shown below the ABC, you can see the situation is similar to the following:
Warning:require (test1aaa.php) [function.require]: failed to open stream:no such file or directory in D:\WebSite\test.php On line 2

Fatal Error:require () [function.require]: Failed opening required ' test1aaa.php ' (include_path= '); C:\php5\pear ') in D:\WebSite\test.php on line 2

Now it is clear to know the difference between include and require: include introduces the file, if encountered error, will give a hint, and continue to run the following code, require the introduction of the file, if encountered error, will give a hint, and stop running the code below.

The difference between include () and require () in PHP

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.