PHP引用文件

引用文件:指将另一个源文件的全部内容包含到当前源文件中进行使用。引用文件可以减少代码的重用性。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>
利用以上代码进行引用文件。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值