Differences in the use of include and require in PHP

Source: Internet
Author: User
The usage of include and require in PHP is different from that 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, but there is a difference in the usage of the two: (include PHP include and require usage difference)

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 require () is an unconditional include function)

1. use different methods

(1) how to use require, such as 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. errors are reported in different ways during execution.

Difference Between include and require: if an error occurs when the include file is introduced, a prompt will be provided and the following code will be run. If an error occurs when the require file is introduced, 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
Include ("test3.php ");
Echo "abc ";
?>

Test2.php
Require ("test3.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 (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 2
Abc (the following code is executed)

Browsing the second file, because the test3.php file is not found, we can see the error message, but the error message below does not show abc, 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 ended directly.

In short, the calling during include is a process behavior with conditions, while require is a preset behavior with no conditions.


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.