Phpinclude and require usage. There are two methods to reference files: require and include. The two methods provide different elasticity. The use of require is as follows: require (MyRequireFile. php );. This function is usually put in two methods to reference files: require and include. The two methods provide different elasticity.
The use of require is as follows: require ("MyRequireFile. php ");. This function is usually placed at the beginning of the PHP program. before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages.
The include () and require () functions are basically the same (include), but there are also some differences in usage. include () is a conditional include function, while require () it is an unconditional include function. For example, in the following code, if the variable $ a is true, it will contain the file a. php:
The code is as follows: |
|
If ($ ){ Include ("a. php "); } |
Include usage methods such as include ("mydomaindefile. php ");. This function is generally placed in the process of process control. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution.
The require () is different from include (). No matter what the value of $ a is, the following code will include file a. php into the file:
The code is as follows: |
|
If ($ ){ Require ("a. php "); } |
For error handling, use the include statement. If an include error occurs, the program skips the include statement. Although the error message is displayed, the program continues to execute! But requre will give you a fatal error.
Error
Use an example to write two php files named test1.php and test2.php. Note that there is no file named test999.php in the same directory.
The code is as follows: |
|
Test. php Include ("test999.php "); Echo "abc "; ?> Test2.php Require ("test999.php ") Echo "abc "; ?> |
Browse the first file. because the test999.php file is not found, we can see the error message. at the same time, abc is displayed under the error message, which may be similar to the following:
Warning: include (test1aaa. php) [function. include]: failed to open stream: No such file or directory in D: WebSitetest. php on line 2
Warning: include () [function. include]: Failed opening 'test1aaa. php' for future Sion (include_path = '.; C: php5pear') in D: WebSitetest. php on line 2
Abc
Browsing the second file, because the test999.php file is not found, we can see the error message. However, the error message below does not show abc. what you see may be similar to the following:
Warning: require (test1aaa. php) [function. require]: failed to open stream: No such file or directory in D: WebSitetest. php on line 2
Fatal error: require () [function. require]: Failed opening required 'test1aaa. php' (include_path = '.; C: php5pear') in D: WebSitetest. php on line 2
And include. The two methods provide different elasticity. The use of require is as follows: require (MyRequireFile. php );. This function is usually placed in...