An in-depth analysis of the usage of require and include _php tutorials

Source: Internet
Author: User

In PHP, the role of include and require is more confusing. Let me take a classic example to illustrate their differences.

When we visit a database frequently, we can write a library statement into a file

con_db.php

$DBH = mysql_connect (localhost,,);
mysql_select_db (ADMREQS);
?>

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

such as require ("con_db.php") or include ("con_db.php")

At this point, the effect of the two functions is similar.

But if this is used

filename.php

Require ("con_db.php")
function Myfun ($par 1, $par 2)
{contains statements processed against the database}
.....
Myfun ($par 1, $par 2);
.....
Myfun ($p 1, $p 2);
?>

Files to Myfun will not be able to continue because the function cannot get outside variables (the include is the same). Unless you pass $DBH as a variable to the function. This in turn increases the complexity of the calling function.

We can solve this problem by putting require or include in the function.

With include, the file's first function call will pass smoothly, but the second call will fail because it cannot be opened once when the database is closed, that is, con_db.php executed two times. Replace the include with require, everything is fine.

That is, require is similar to a pre-scan, and in the execution of a program, the require file is executed and executed only once, regardless of function or function. The include is called once every execution of the file, that is, after the execution of the next execution here, will still be executed again.

So, if in a loop, some statements you just want to do once, then you can include them with require.

http://www.bkjia.com/PHPjc/508257.html www.bkjia.com true http://www.bkjia.com/PHPjc/508257.html techarticle in PHP, the role of include and require is more confusing. Let me take a classic example to illustrate their differences. When we visit a database frequently, we can put the Lianku language ...

  • 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.