Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 10:03:59
Original
864 people have browsed it

本文章详细的介绍了关于在php中的文件调用测试包括了include(), require() ,include_once(),require_once()等多种交换调用,有需要的朋友可以参考一下。


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

 代码如下 复制代码

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template