php中的include和require的区别

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

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 ?

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.

php中的include和require的区别

时间: 2024-10-14 05:17:15

php中的include和require的区别的相关文章

PHP中include和require的区别详解

1.概要  require()语句的性能与include()相类似,都是包括并运行指定文件.不同之处在于:对include()语句来说,在执行文件时每次都要进行读取和评估:而对于require()来说,文件只处理一次(实际上,文件内容替换require()语句).这就意味着如果可能执行多次的代码,则使用require()效率比较高.另外一方面,如果每次执行代码时是读取不同的文件,或者有通过一组文件迭代的循环,就使用include()语句. require的使用方法如:require("myfil

辛星与您彻底分析PHP中的include和require等的区别

首先说一下require吧,我们知道如果它引入的文件不存在,将会导致程序无法继续执行,因此它通常放在程序的最前面,通常是一些特别重要的部分,比如连接数据库库,比如加载配置文件,比如引用核心函数库等等. 然后说一下include把,它引入成功与否并不重要,因此它和html的特点很接近,可以用于程序的开头,当然很多时候我们是需要引用的时候才include进来,这样,它的位置也就更加随意一些,可以放在中间. 还有一个include_once,它的开销比include 大得多,为什么呢,因为它会检测该文

php中include()和require()的区别

1.引用文件方式 对 include()来说,在include()执行时文件每次都要进行读取和评估:而对于require()来说,文件只处理一次(实际上,文件内容替换 了require()语句.这就意味着如果有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高.另一方面,如果每次执行代码时读取不同的文件,或者有通过一组文件叠代的循环,就使用include(),因为可以给想要包括的文件名设置一个变量. 2.是否有条件引用 在PHP变成中,include()与requir

include和require的区别

细节决定成败! 1.引用文件方式 对include()来说,在include()执行时文件每次都要进行读取和评估:而对于require()来说,文件只处理一次(实际上,文件内容替换了require()语句).这就意味着如果有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高.另一方面,如果每次执行代码时相读取不同的文件,或者有通过一组文件叠代的循环,就使用include(),因为可以给想要包括的文件名设置一个变量,当参数为include()时使用这个变量. 2.是否有

包含文件函数include与require的区别

include或include_once一般用于动态包含,所谓动态包含就是根据不同条件包含不同文件 require或require_once一般用于静态包含,比如包含一个html文件的头部或者尾部 如:require_once footer.inc.php include和require可以包含多次,include_once和require_once只包含一次 包含文件函数include与require的区别,布布扣,bubuko.com

PHP中include()与require()的区别

require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份.常用的函数,亦可以这个方法将它引入网页中. include 使用方法如 include("MyIncludeFile.php"); .这个函数一般是放在流程控制的处理部分中.PHP 程序网页在读到 include 的文件时,才将它读进

PHP中include()与require()的区别说明

require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份.常用的函数,亦可以这个方法将它引入网页中. include 使用方法如 include("MyIncludeFile.php"); .这个函数一般是放在流程控制的处理部分中.PHP 程序网页在读到 include 的文件时,才将它读进

php 中 include 与 require 的区别

以下内容转自:https://blog.csdn.net/hsd2012/article/details/51089785 网上太多关于php中include与require区别.其实说的都是经不起验证的.随意找了一个截图如下: 还信誓旦旦的解释:include()是有条件包含函数,而require()则是无条件包含函数. 但是这是很久之前的事了,现在的php,require与include确实有区别,笔者测试的是5.3以上版本测试结果如下: 发现这两种结构除了在性能和在如何处理包含失败之外,其

PHP 中 include 和 require 的区别详解

前言 在做程序设计的时候避免不了要去引用外部文件,在 PHP 中引入文件的方式有很多种,这里详细说一下 include :require :include_once:require_once. require () 语句的性能与 include () 相类似,都是包括并运行指定文件.除了处理失败的方式不同之外.require在出错时产生 E_COMPILE_ERROR 级别的错误,终止脚本运行:而 include 只产生警告(E_WARNING),脚本会继续运行. 1.include 和 req