Difference in php include () and require () of

1. The reference papers

    To include (), in include () is executed every time a file is read and assessment; and for the require (), the file is only processed once (in fact, replace the contents of the file require () statement here. if the code means comprising one of these instructions may be executed multiple times and code use require () more efficient. on the other hand, if the read code is different for each execution file, or by a group with a file iterative loop, use the include (), because you can include the file name you want to set a variable.

 

2. whether conditional references

    In PHP becomes, the same functions include () and require (), but there are some differences in usage, include () is a function of conditions comprising, but require () function is unconditionally comprising. For example, in an example below, if the variable $ somgthing is true, then the file somefile comprising:

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

But no matter what value $ something, the following code will be included into the file somefile file:

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

The following interesting example of this fully explains the difference between these two functions.

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

In this code, each cycle, the program will include all go in the same file. Obviously this is not the programmer's mind, we can see from the code in the hope that this code at each cycle, the different files included. To accomplish this, we must help function include ():

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

3. error

    To speak with an example, write two php files, named test1.php and test2.php, pay attention to the same directory, there is not a file name is test999.php.

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

Browse the first file, because the file is not found test999.php, we see the error message, at the same time, the error message below shows abc, you may see a situation similar to the following:
Warning: the 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 second file, because the file is not found test999.php, we see the error message, however, below error message does not appear abc, you may see a situation similar to the following:
Warning: The 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 we can clearly know the difference between include and require are: include when introducing file, if an error is encountered, will give prompt and continue to run the code below, require the introduction of the document, if an error is encountered, gives prompt, and stop running the code below.

Reproduced in: https: //www.cnblogs.com/wangzehuaw/p/6144764.html

Guess you like

Origin blog.csdn.net/weixin_34186950/article/details/93778506