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.