Home > Article > Backend Development > Detailed explanation of the difference between require and include in PHP 5.2.x
Let’s look at an example first:
1. conn.php file:
Copy code code example:
<?php $conn = mysql_connect('localhost','',''); mysql_select_db('php'); ?>
2. Call as follows:
Copy code code example:
function fun($a) { include("conn.php"); .......//数据库处理语句 } .... fun("aaa");//第一次调用 ..... fun("bbb");//第二次调用
An error will occur during the second call. If you replace include with require, there will be no problem.
Because the require file is only called once when reading the page, and include is called every time it calls a place containing fun, so there is an error of connecting again before the database connection is closed.
Therefore, you only need to call require once in the loop , and both of them can be used in other places.
3. include() generates a warning and require() results in a fatal error.
In other words, if you want to stop processing the page when a missing file is encountered, use require().
When using include(), the script will continue to run.
The above is my experience on the difference between require and include when using PHP 5.2.x. I would like to share it with you. I hope it will be of some help to you.
The above is the detailed content of Detailed explanation of the difference between require and include in PHP 5.2.x. For more information, please follow other related articles on the PHP Chinese website!