Home >Backend Development >PHP Tutorial >Detailed explanation of the usage differences between include and require in PHP
Example 2, but no matter what the value of $something is, the file somefile will be included in the file:
Example 3 fully illustrates the difference between these two functions.
in this code , the program will include the same file each time it loops. From the code, you can see that this code hopes to include different files each time it loops. If you want to complete this function, you must turn to the function include():
2, when executing Different error reporting methods The difference between include and require: When include introduces a file, if it encounters an error, it will give a prompt and continue to run the code below. When require introduces a file, if it encounters an error, it will give a prompt and stop running the code below. example: Write two php files named test1.php and test2.php. Note that there should not be a file named test3.php in the same directory. test1.php
test2.php
Code description: Browse the first file. Because the test999.php file was not found, an error message appeared. At the same time, abc was displayed below the error message. What you saw may be similar to the following: Warning: include(test3.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 ‘test3.php’ for inclusion (include_path=’.;C:php5pear’) in D:WebSitetest.php on line 2 abc (the following is executed) Browse the second file. Because the test3.php file was not found, an error message appeared. However, abc was not displayed below the error message. What you saw may be similar to the following: Warning: require(test3.php) [function.require]: failed to open stream: No such file or directory in D:WebSitetest2.php on line 2 Fatal error: require() [function.require]: Failed opening required 'test3.php' (include_path='.;C:php5pear') in D:WebSitetest.php on line 2 The following is not executed and ends directly. In short, what is called during include execution is a process behavior and conditional, while require is a preset behavior and unconditional. |