php中文件包含include(),require()介绍

作者:袖梨 2022-06-24


7.3.1 Include的使用,可以包含相同的文件多次

 代码如下 复制代码

php

include 'demo1.php';

include 'demo1.php';

include 'demo1.php';

?>

 

输出结果如

 代码如下 复制代码

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
 

 

7.3.2 include_once使用上和include没什么区别,但是调用多次只会包含相同的文件一次

 

 代码如下 复制代码

 

include_once 'demo1.php';

include_once 'demo1.php';

include_once 'demo1.php';

?>

 

结果如下

 代码如下 复制代码
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
 

 

7.3.3 require() 语句包含并运行指定文件。

 代码如下 复制代码

require 'demo1.php';

require 'demo1.php';

require 'demo1.php';

?>
 

结果如下

 代码如下 复制代码

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
 

 

7.3.4 require_once() 语句在脚本执行期间包含并运行指定文件.但是不重复包含相同的文件。

 代码如下 复制代码

require_once 'demo1.php';

require_once 'demo1.php';

require_once 'demo1.php';

?>

 

输出结果如下

 代码如下 复制代码
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
 

 

7.3.5 include与require的区别

Include后面如果还有其他代码,当调用include出错时,后面的代码还会继续执行,但是require则不会。

 

Include在调用一个不存在的文件时,会给出警告,但是会继续执行后面的代码。

 代码如下 复制代码

include 'demo111.php';

 

echo('this is demo13.php');

?>
 

输出结果如下

 

 代码如下 复制代码

Warning: include(demo111.php) [function.include]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo13.php on line 2

Warning: include() [function.include]: Failed opening 'demo111.php' for inclusion (include_path='.;C:php5pear') in D:AppServwwwBasic7demo13.php on line 2
this is demo13.php

 

Require在调用一个不存在的文件时,会给出一个错误,并中止代码的执行。

 代码如下 复制代码

require 'demo111.php';

 

echo('this is demo14.php');

?>
 

输出结果如下

 

 代码如下 复制代码

Warning: require(demo111.php) [function.require]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo14.php on line 2

Fatal error: require() [function.require]: Failed opening required 'demo111.php' (include_path='.;C:php5pear') in D:AppServwwwBasic7demo14.php on line 2

相关文章

精彩推荐