In PHP, include () and require () have the same functions. include (include_once) and require (require_once) both read the included file code to the specified location, however, there is a difference in their usage: (include () is a conditional include function, while requ is in PHP, include () and require () have the same function, include (include_once) and require (require_once) both read the included file code to the specified location, but there is a difference in usage between the two: (include () is a conditional inclusion function, while require () is unconditional include function) 1. usage is different (1) the use of require is as follows: require ("requireFile. 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 a commonly used function to a webpage. The introduction is unconditional. it occurs before the program is executed, whether or not the conditions are true or not. (it may not be executed ). (2) include usage methods such as include ("includeFile. php ");. This function is generally placed in the process section of process control. The include file is read on the PHP webpage. In this way, you can simplify the process during program execution. The introduction is conditional. when the program is executed, the import is performed only when the condition is set (the code generated by compilation can be simplified ). For example, in the following example, if the variable $ somgthing is true, it will contain the file somefile: if ($ something) {include ("somefile ");} however, no matter what value $ something takes, the following code will include the file somefile into the file: if ($ something) {require ("somefile ");} the following interesting example fully demonstrates the differences between the two functions. $ I = 1; while ($ I <3) {require ("somefile. $ I "); $ I ++;} in this code, each loop contains the same file. Obviously, this is not the original intention of the programmer. from the code, we can see that this code will include different files in each loop. To complete this function, you must turn to the include () function: $ I = 1; while ($ I <3) {include ("somefile. $ I "); $ I ++;} 2. when an error is reported, the difference between include and require is: if an error occurs when the include file is introduced, a prompt is displayed and the following code is run. when the require file is introduced, if an error occurs, a prompt is displayed and the following code is stopped. For 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 Test2.php 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. what you see may be 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 (executed below) browses the second file. because the test3.php file is not found, we can see the error message. However, the error message is returned. The following section does not show abc. what you see may be 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 is not executed and ended directly. In short, what is called during include is a process action with conditions, while require is a preset action and unconditional.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.