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

PHP Include与require

发布时间:2016-10-27   编辑:jiaochengji.com
教程集为您提供PHP Include与require 等资源,欢迎您收藏本站,我们将为您提供最新的PHP Include与require 资源

PHP Include 文件

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

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

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

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

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

<html>
<body><?php include("header.php"); ?><h1>Welcome to my home page</h1><p>Some text</p></body>
</html>

示例2
现在,让我们假设我们有一个标准的菜单文件,应当使用的所有网页(包括文件通常有。 “ PHP的”扩展) 。看看“ menu.php ”文件如下:

<html>
<body><a href="http://www.w3schools.com/default.php">Home</a> |
<a href="http://www.w3schools.com/about.php">About Us</a> |
<a href="http://www.w3schools.com/contact.php">Contact Us</a>

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

<pre><?php include("menu.php"); ?></pre> <pre><h1>Welcome to my home page</h1></pre> <pre><p>Some text</p></pre> <pre></body> </html></pre> <pre>

如果你看看源代码的“ default.php ”在浏览器中,它将看起来就像这样:

<table class="ex" id="table3" cellspacing="0" width="100%" border="1"><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>

当然,我们也必须做同样的事情为“ about.php ”和“ contact.php ” 。通过使用包括文件,

您只需更新中的文字“ menu.php ”文件如果您决定重新命名或更改顺序的联系或添加其他网页的网站。

The require()

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

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

PHP代码:

<html>
<body>

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

</body>
</html>

 

error

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

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

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

PHP代码:

 

 <html>
<body>

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

</body>
</html>

<pre>Warning: require(wrongFile.php) [function.require]: failed to open stream: No such file or directory in C:homewebsitetest.php on line 5</pre> <pre>Fatal error: require() [function.require]: Failed opening required 'wrongFile.php' (include_path='.;C:php5pear') in C:homewebsitetest.php on line 5</pre> <pre> </pre> <pre>
回声说法是不执行的,因为脚本执行停止后致命错误。

这是推荐使用的需要( )函数而不是包括( ) ,因为脚本不应该继续执行,如果文件丢失或命名错误。
</pre>
</pre>
</pre>

您可能感兴趣的文章:
php中的include()与require()的对比分析
include()与require()的对比
php include与require用法介绍
php 5.2.x中require、include的区别
PHP文件包含语句 include、include_once、require、require_onc
include,require,以及后缀加once的区别
php require和include的区别
详解PHP中include与require的用法区别
php中require和include的区别是什么?
php中的include,require,include_once,require_once

[关闭]
~ ~