Php5.2.x: differences between require and include

Source: Internet
Author: User
Example: 1. This is the content of conn. php :? Php $ connmysql_connect (localhost,); mysql_select_db (php );? 2. Call functionfun ($ a) {include (conn. php );....... database processing statement }..... fun (aaa); First Time

Example: 1. This is the content of conn. php :? Php $ conn = mysql_connect ('localhost', '',''); mysql_select_db ('php ');? 2. If you call function fun ($ a) {include (conn. php );....... // database processing statement }..... fun (aaa); // The First Time

Example:

1. This is the content of conn. php:

$ Conn = mysql_connect ('localhost ','','');
Mysql_select_db ('php ');
?>

2. If the call is as follows:

Function fun ($)
{
Include ("conn. php ");
... // Database processing statement

}

.....

Fun ("aaa"); // The first call

.....

Fun ("bbb"); // second call


An error occurs during the second call, but changing include to require will not cause any problem.

Because the require file is called only once when the page is read, and the include file is called in every call that contains fun, the database connection is re-connected before it is closed.

Therefore, you only need to call require once in a loop. Both can be used elsewhere.

3. include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (). This is not the case with include (). The script will continue to run.

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.