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.