The difference between PHP-include () and require () of

1. The reference papers

To include (), in include () when performing file every time to read and evaluate ;
to require (), the file is only processed once (in fact, replace the contents of the file require () statement this. if the code means comprising one of these instructions may be executed multiple times and code use require () more efficient.
On the other hand, to read a different file if the code is executed each time through a set of files, or have iterative loop, use the include (), because you can set a variable to the file name you want to include.

2. whether conditional references

In PHP, 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 the include ( "somefile");}. 3
but no matter what value $ something, the following code somefile file into the file contains:
. 1 IF ($ something) {2 the 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 have the same file included. 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 function, the function must help the include ():
. 1. 1 = $ I; 2 the while ($ I <. 3). 3 {the include ( "I $ somefile."); I $. 4 ++;}. 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: 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: 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:

When introduced include file, if an error is encountered, it will give prompt and continue to run the code below,

require the introduction of the document, if an error is encountered, will give prompt, and stop running the code below.


 

发布了47 篇原创文章 · 获赞 3 · 访问量 1951

Guess you like

Origin blog.csdn.net/yueyekonglong/article/details/104025187