Include (或 require)语句效果相同都是获取指定文件内容包含进来,可用于创建重复页面,例如后端的函数或前端导航、底部信息等。
include 语法
include '文件名'; |
require 语法
require '文件名'; |
head.php文件
<?php echo "<p>我是头部菜单</p>"; ?>
index.php文件
<html> <body> <?php include 'head.php';?> <p>我是页面bady</p> <p>内容</p> </body> </html>
我是头部菜单 我是页面bady 内容 |
name.php文件
<?php $name='炫代码'; $url='http://www.xuandaima.com'; ?>
body.php文件
<html> <body> <h1>首页!</h1> <?php require 'name.php'; echo "本网站名是:" . $name . $url "。"; ?> </body> </html>
首页! 本网站名是:炫代码http://www.xuandaima.com 路径写法: |
<?php include './head.php';//相对路径,当前目录下的head.php include 'E:\www\test\head.php';//绝对路径,指定目录下的head.php ?>
include发生错误时,发出警告并 继续执行
require发生错误时,发出警告并 停止执行
用Include包含一个不存在的test.php文件后,在警告后还是输出了"string"
<?php include 'noFileExists.php'; echo "string"; ?>
用Include包含一个不存在的test.php文件后,在警告后没有输出
<?php require 'noFileExists.php'; echo "string"; ?>