您的位置:首页 > 编程语言 > PHP开发

PHP引用文件

2014-08-08 09:33 169 查看
引用文件:指将另一个源文件的全部内容包含到当前源文件中进行使用。引用文件可以减少代码的重用性。PHP提供了include语句、include_once语句、require语句和require_once语句用于实现引用文件。

引用1:include语句

特点:在使用include语句引用外部文件时,只有代码执行到include语句时才将外部文件引用进来并读取文件的内容,当所引用的外部文件发生错误时,系统只给出一个警告,而整个PHP文件则会继续向下执行。

语法:void include(string filename); 其中参数filename是指定的完整路径文件名。

引用2:include_once语句

特点:与include语句的区别是:include_once语句会在导入文件前先检测文件是否在该页面的其他部分被引用过,如果有,则不会重复引用该文件。

语法:void include_once(string filename);

引用3:require语句

特点:在PHP文件被执行前,PHP解析器会用被引用的文件的内容全部内容替换require语句,然后与require语句之外的其他语句组成新的PHP文件,最后再按最新的PHP文件执行代码。

语法:void require(string filename);

引用4:require_once语句

特点:与require语句的区别是检查该文件是否被重复引用。如果有,则不会再次重复调用该文件。

语法:void require_once(string filename);

举例说明

创建4分文件:quotes.php  first.php second.php third.php forth.php

(1)quotes.php文件

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>应用include语句引用外部文件</title>
</head>
<body>
<table width="975" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><?php include("first.php");?></td>
</tr>
<tr>
<td><?php include_once("second.php");?></td>
</tr>
<tr>
<td><?php require("third.php");?></td>
</tr>
<tr>
<td><?php require_once("forth.php");?></td>
</tr>
</table>
</body>
</html>


(2)first.php文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
margin-top: 0px;
margin-bottom: 0px;
}
-->
</style>
</head>
<body>
<table width="975" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="50" width="600" bgcolor="#CC3300" align="center">我是include语句引入的 </td>
</tr>
</table>
</body>
</html>


(3)second.php文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
margin-top: 0px;
margin-bottom: 0px;
}
-->
</style>
</head>
<body>
<table width="975" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="50" width="600" bgcolor="#CC3390" align="center">我是include_once语句引入的 </td>
</tr>
</table>
</body>
</html>


(4)third.php文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
margin-top: 0px;
margin-bottom: 0px;
}
.style3 {font-size: 20px; font-family: "隶书"; color: #FF3366;}
-->
</style>
</head>
<body>
<table width="975" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="50" width="600" bgcolor="#00CCFF" align="center">我是require语句引入的</td>
</tr>

</table>
</td>
</tr>
</table>
</body>
</html>


(5)forth.php文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
margin-top: 0px;
}
-->
</style></head>
<body>
<table width="975" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="50" width="600" bgcolor="#FFCCCC" align="center">我是require_once语句引入的</td>
</tr>
</table>
</body>
</html>
利用以上代码进行引用文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息