Heim > Artikel > Backend-Entwicklung > php中include()和require()以及include_once()和require_once()的区别
4者都有包含文件的意思,但include()和include_once()在包含文件出错时程序会继续往下执行,而require()和require_once()则不会,另外include_once()和require_once()只包含一次,多余的不会被包含进来。例子:
$a=5;
//include('./test.php');//test.php中代码为$a+=3;
//require('./test.php');
//echo $a;//结果为8;
//包含不存在的文件test1.php时
//include('./test1.php');//结果提示warning,并输出5;
//require('./test1.php');//结果提示错误fatal error并停止程序往下执行
//echo $a;
include_once('./test.php');
include_once('./test.php');
include_once('./test.php');
include_once('./test.php');
echo $a;
//结果输出8,因为include_once()或者require_once()如果有多个,只会执行第一个。而include()或者require()如果有多个则会全部执行