Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between require and include in PHP 5.2.x

Detailed explanation of the difference between require and include in PHP 5.2.x

零下一度
零下一度Original
2017-06-17 16:43:481132browse

Let’s look at an example first:
1. conn.php file:

Copy code code example:

<?php
    $conn =  mysql_connect(&#39;localhost&#39;,&#39;&#39;,&#39;&#39;); 
    mysql_select_db(&#39;php&#39;); 
?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn