PHP tips: analyze the use of require and include through the instance in depth

Source: Internet
Author: User
In php, the role of include and require is relatively easy to mix. The following is a classic example to illustrate their differences. When we often visit a database, we can write the database connection statement into a file con_db.php? Php $ dbh = mysql_connec

In php, the role of include and require is relatively easy to mix. The following is a classic example to illustrate their differences.

When we often visit a database, we can write the database connection statement as a file.

Con_db.php

$ Dbh = mysql_connect ('localhost ','','');
Mysql_select_db ('admreqs ');
?>

In actual use, we can call this file in the program.

Such as require ('con _ db. php') or include ('con _ db. php)

At this time, the consequences of the two functions are less than a few.

However, if this is the case

Filename. php

Require ('con _ db. php ')
Function myfun ($ par1, $ par2)
{Contains statements for database processing}
.....
Myfun ($ par1, $ par2 );
.....
Myfun ($ p1, $ p2 );
?>

Files cannot be executed continuously at myfun, because the function cannot obtain external variables (the same is true for include ). Unless $ dbh is used as a variable to pass to the function. This increases the complexity of calling functions.

We can solve this title by placing require or include in the function.

If include is used, the first function call of the file will pass smoothly, but the second call will not be executed because it cannot be opened once when the database is not closed. that is to say, con_db.php performed twice. Replace include with require. everything works normally.

That is to say, require is similar to a pre-scan. when a program is executed, The require file will be executed only once, no matter in the function or outside the function. The include statement calls a file every time a file is executed, that is, after this fulfillment, the file will be executed again next time.

Therefore, if you want to execute some statements only once in a loop, you can use require to include them.

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.