php中的include和require的区别

简介: <p class="intro" style="line-height:18.200000762939453px; font-family:verdana,helvetica,arial,sans-serif; font-size:14px; margin-top:10px"> <span style="color:#ff0000">主要关注红色标记语句即可。</span></p> <

主要关注红色标记语句即可。

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


PHP include and require Statements

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Syntax

include ' filename';

or

require ' filename';


PHP include Examples

Example 1

Assume we have a standard footer file called "footer.php", that looks like this:

<?php
echo "<p>Copyright &copy; 1999-" . date("Y") . " W3Schools.com</p>";
?>

To include the footer file in a page, use the include statement:

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>

</body>
</html>

Run example »


Example 2

Assume we have a standard menu file called "menu.php":

<?php
echo '<a href="/default.asp">Home</a> -
<a href="/html/default.asp">HTML Tutorial</a> -
<a href="/css/default.asp">CSS Tutorial</a> -
<a href="/js/default.asp">JavaScript Tutorial</a> -
<a href="default.asp">PHP Tutorial</a>';
?>

All pages in the Web site should use this menu file. Here is how it can be done (we are using a <div> element so that the menu easily can be styled with CSS later):

Example

<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

Run example »


Example 3

Assume we have a file called "vars.php", with some variables defined:

<?php
$color='red';
$car='BMW';
?>

Then, if we include the "vars.php" file, the variables can be used in the calling file:

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php include 'vars.php';
echo "I have a $color $car.";
?>

</body>
</html>

Run example »


PHP include vs. require

The require statement is also used to include a file into the PHP code.

However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php include 'noFileExists.php';
echo "I have a $color $car.";
?>

</body>
</html>

Run example »

If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:

Example

<html>
<body>

<h1>Welcome to my home page!</h1>
<?php require 'noFileExists.php';
echo "I have a $color $car.";
?>

</body>
</html>

Run example »

Note Use require when the file is required by the application.

Use include when the file is not required and application should continue when file is not found.
相关文章
ly~
|
3月前
|
安全 Java 大数据
php跟java有什么区别
PHP 和 Java 是两种常用编程语言,各有特色。PHP 语法简洁灵活,适用于快速开发中小型网站,尤其在 Web 脚本和数据库交互中表现出色。Java 则语法严谨,强类型特性使其在企业级应用、移动开发及大数据处理中更受欢迎,具备高稳定性和安全性。通过优化,PHP 性能可提升,而 Java 在大规模应用中表现更佳。总体而言,PHP 开发效率高但维护性稍差,Java 则更注重代码质量和安全性。
ly~
79 5
|
3月前
|
SQL 关系型数据库 数据库连接
php连接数据库之PDO,PDO的简单使用和预定义占位符的使用以及PDOStatement对象的使用,占位符的不同形式,bindValue和bindParam绑定预定义占位符参数的区别
本文介绍了PHP中PDO(PHP Data Objects)扩展的基本概念和使用方法。内容包括PDO类和PDOStatement类的介绍,PDO的简单使用,预定义占位符的使用方法,以及PDOStatement对象的使用。文章还讨论了绑定预定义占位符参数的不同形式,即bindValue和bindParam的区别。通过具体示例,展示了如何使用PDO进行数据库连接、数据查询、数据插入等操作。
php连接数据库之PDO,PDO的简单使用和预定义占位符的使用以及PDOStatement对象的使用,占位符的不同形式,bindValue和bindParam绑定预定义占位符参数的区别
|
6月前
|
PHP
PHP &&是什么,||是什么,&&和||有什么区别?
PHP &&是什么,||是什么,&&和||有什么区别?
55 1
|
7月前
|
PHP
PHP public、protected、private、static、abstract、final、interface、implements 区别对比
PHP public、protected、private、static、abstract、final、interface、implements 区别对比
98 0
|
PHP
PHP - Laravel Blade模板注释 {{-- 注释 --}} 与 <!-- 注释 --> 的区别
PHP - Laravel Blade模板注释 {{-- 注释 --}} 与 <!-- 注释 --> 的区别
99 0
|
机器学习/深度学习 安全 Java
PHP, Python和Java的区别
PHP, Python和Java是广泛使用的编程语言。每种编程语言都有其独特的优点和缺点。在本文中,我们将对这些编程语言进行分析,并探讨它们在不同应用场景中的最佳用途。
165 0
|
7月前
|
PHP 调度
PHP CGI、FastCGI、PHP-FPM、PHP-CGI 区别
PHP CGI、FastCGI、PHP-FPM、PHP-CGI 区别
80 5
|
PHP
php中elseif 和else if的区别
@(黑线)以前貌似看到if这里,就直接跳了,可是今天再读文档,又发现了点东西... @(乖)可能你已经知道了php存在elseif 和else if,保留了c的风格。
77 0