教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php include 与require 教程

php include 与require 教程

发布时间:2016-10-27   编辑:jiaochengji.com
教程集为您提供php include 与require 教程等资源,欢迎您收藏本站,我们将为您提供最新的php include 与require 教程资源
在php中我们调用外部文件都会用到php include 与require 来包含进来了,但是否include 和require他们两有什么区别呢

服务器端include(小型工业)用于创建功能,页眉,页脚,或内容,将重复使用的多个页面。


服务器端include
您可以插入的内容的文件到PHP文件之前,服务器执行它,与include( )或require( )函数。这两项职能是相同的各种方式,但他们如何处理错误。在include( )函数生成一个警告(但该脚本将继续执行) ,而需要( )函数生成一个致命的错误(和脚本执行后,将停止错误) 。

这两项职能是用于创建功能,页眉,页脚,或内容,可重复使用的多个页面。

这可以节省开发了相当多的时间。这意味着,您可以创建一个标准的标题或菜单文件,您想您的所有网页,include。当头需要更新,您只能更新一个include文件,或当你添加了新的一页到您的网站,您可以改变菜单文件(而不是更新的所有链接的网页) 。


在include( )函数
在include( )函数中的所有文本指定的文件并复制到文件,include使用功能。

范例1
假设您有一个标准的头文件,所谓的“ header.php ” 。include头文件在一个页面上,使用include( )函数,就像这样:

<table style="width: 443px; height: 206px" class="ex" border="1" cellspacing="0" width="443"> <tbody> <tr> <td> <pre><html> <body></pre> <pre><?php include("header.php"); ?></pre> <pre><h1>Welcome to my home page</h1></pre> <pre><p>Some text</p></pre> <pre></body> </html></pre> </td> </tr> </tbody> </table>

这三个文件, “ default.php ” , “ about.php ”和“ contact.php ”大家都应该include“ menu.php ”文件。以下是代码“ default.php ” :

<?php include("menu.php"); ?>

输出.

<table style="width: 450px; height: 160px" id="table3" class="ex" border="1" cellspacing="0" width="450"> <tbody> <tr> <td> <pre><html> <body> <a href="default.php">Home</a> | <a href="about.php">About Us</a> | <a href="contact.php">Contact Us</a> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html></pre> </td> </tr> </tbody> </table>

下面我们再来看看require函数www.jiaochengji.com/

的require( )函数是相同的,include( ) ,只是不同的处理错误。

在include( )函数生成一个警告(但该脚本将继续执行) ,而需要( )函数生成一个致命的错误(和脚本执行后,将停止错误) 。

如果您加入了文件,include( )函数和发生错误时,你可能得到一个错误信息类似下面的一个。

<?php
include("wrongFile.php");
echo "Hello World!";
?>

<pre>Warning: include(wrongFile.php) [function.include]: failed to open stream: No such file or directory in C:homewebsite est.php on line 5</pre> <pre>Warning: include() [function.include]: Failed opening 'wrongFile.php' for inclusion (include_path='.;C:php5pear') in C:homewebsite est.php on line 5</pre> <pre>www.jiaochengji.com/phper/php.html</pre> <pre> </pre> <pre>Hello World!</pre> <pre>

请注意,声明的回音仍是执行!这是因为报警不停止执行脚本。

现在,让我们运行相同的例子与require( )函数。

PHP代码:

<?php require("wrongFile.php"); echo "Hello World!"; ?>

 

提示:

ning: require(wrongFile.php) [function.require]: failed to open stream: No such file or directory in C:homewebsite est.php on line 5

<pre>Fatal error: require() [function.require]: Failed opening required 'wrongFile.php' (include_path='.;C:php5pear') in C:homewebsite est.php on line 5</pre> <pre>转载: www.jiaochengji.com/phper/php.html </pre>
</pre>

您可能感兴趣的文章:
php require和include的区别
include,require,以及后缀加once的区别
php中require和include的区别是什么?
php中的include,require,include_once,require_once
php的use和require的区别
php关于require和include的区别
php include 与require 教程
谈谈PHP中require和include的区别
PHP文件包含语句 include、include_once、require、require_onc
详解PHP中include与require的用法区别

[关闭]
~ ~