In PHP, include () and require () have the same functionality, include (include_once) and require (require_once) are to read the included file code into the specified location, But there is a difference between the two: (Include () is a conditional include function, while require () is an unconditional inclusion function) 1, using different methods (1) require use such as require (" Requirefile.php "); This function is usually placed at the top of the PHP program, before the PHP program is executed, it will read into the file introduced by require, making it a part of the PHP Program Web page. A commonly used function can also be introduced into a Web page in this way. The introduction is unconditional and occurs before the program executes, regardless of whether or not the condition is set up (may not be performed). (2) Include use method such as include ("includefile.php");. This function is typically placed in the processing section of the Process Control. The PHP Program page reads the included file when it is read. This way, you can simplify the process of executing your program. The introduction is conditional, occurs when the program executes, only when the condition is set up (which simplifies compiling the generated code). For example, in the following example, if the variable $somgthing is true, it will include the file Somefile:if ($something) {include ("Somefile");} but regardless of $ Something, the following code will include the file Somefile in the file: if ($something) {require ("Somefile");} The following interesting example illustrates the difference between the two functions. $i = 1; while ($i < 3) {require ("somefile. $i"); $i + +; In this code, each time the loop, the program will include the same file. Obviously this is not the intention of the programmer, and from the code we can see that the code wants to include different files in each loop. If you want to complete this function, you must help function include (): $i = 1; while ($i < 3) {include ("somefile. $i"); $i + +;} 2. Implementation times wrong way different include and require difference: include leadIn the file, if you encounter errors, will give a hint, and continue to run the following code, require introduced the file, if encountered errors, will give a hint, and stop running the code below. For example, the following example: write two php files, named test1.php and test2.php, note the same directory, do not exist a name is test3.php file. test1.php;? PHP include ("test3.php"); echo "abc";?> test2.php PHP require ("test3.php") echo "abc";?> Browse the first file, Because did not find the test999.php file, we saw the error information, at the same time, the error message below the ABC, you can see is similar to the following situation: Warning:include (test3.php) [function.include]: Failed to open stream:no such file or directory into D:\WebSite\test.php on line 2 warning:include () [Function.incl Ude]: Failed opening ' test3.php ' for inclusion (include_path= '.; C:\php5\pear ') in D:\WebSite\test.php of Line 2 ABC (executed below) Browse the second file, Because did not find the test3.php file, we saw the error message, but, the error message does not show the bottom of ABC, you can see is similar to the following situation: Warning:require (test3.php) [function.require]: Failed to open stream:no such file or directory into 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 of line 2 The following is not executed, directly ending in summary, when the include is invoked when executing, is a process behavior, conditional, and require is a preset behavior, 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.