- If ($ something ){
- Include ("somefile ");
- }
Example 2: no matter what value $ something takes, the file somefile will be included in the file:
- If ($ something ){
- Require ("somefile ");
- }
Example 3 illustrates the differences between the two functions.
- $ I = 1;
- While ($ I <3 ){
- Require ("somefile. $ I ");
- $ I ++;
- }
In this code, each loop contains the same file. From the code, we can see that this code requires different files to be included in each loop. To complete this function, you must turn to the include () function ():
- $ I = 1;
- While ($ I <3 ){
- Include ("somefile. $ I ");
- $ I ++;
- }
2. errors are reported in different ways. Difference Between include and require: if an error occurs when the include file is introduced, a prompt is displayed and the following code is continued. If an error occurs when the require file is introduced, a prompt is displayed, and stop running the following code. Example: write two php files named test1.php and test2.php. Note that there is no file named test3.php in the same directory. Test1.php
- Include ("test3.php ");
- Echo "abc ";
- ?>
Test2.php
- Require ("test3.php ")
- Echo "abc ";
- ?>
Code description: View the first file. because the test999.php file is not found, an error message is displayed. at the same time, abc is displayed under the error message. you may see a situation similar to the following: Warning: include (test3.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 'test3. php 'for declaration (include_path = '.; c: \ php5 \ pear ') in D: \ WebSite \ test. php on line 2abc (the following code is executed) Browsing the second file, because the test3.php file is not found, an error message is displayed. However, abc is not displayed at the bottom of the error message. you may see a situation similar to the following: Warning: require (test3.php) [function. require]: failed to open stream: No such file or directory in D: \ WebSite \ test2.php on line 2 Fatal error: require () [function. require]: Failed opening required' test3. php '(include_path = '.; c: \ php5 \ pear ') in D: \ WebSite \ test. php on line 2 The following is not executed, and it ends directly. In short, the calling during include is a process behavior with conditions, while require is a preset behavior with no conditions. |