首页  >  文章  >  后端开发  >  PHP Include 和 Require 函数

PHP Include 和 Require 函数

WBOY
WBOY原创
2024-08-29 12:56:39174浏览

PHP Include 和 Require 语句有助于获取特定 PHP 文件/doc 中存在的所有代码/文本/标记代码,然后它将代码文本从包含的文件复制到 PHP Include 语句所在的文件被使用。如果将 include.php 文件中包含的脚本复制到原始 PHP 文件,则输出将与上述描述中提到的相同。为了区分大型项目的程序,必须创建不同的程序。要将所有这些文件组装在一个文件中,应在原始索引或其他 PHP 程序文件中使用 include 或 require 语句。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

include("Enter File Path – Pavan Kumar Sake"); or include "Enter File Path – Pavan Kumar Sake";
require("Enter the File Path – Pavan Kumar Sake"); or require "Enter the File Path – Pavan Kumar Sake";

include 和 require 在 PHP 中如何工作?

此语句将有助于允许包含在特定原始 PHP 文件中的 other.PHP 文件中存在的代码。如果我们直接使用原始 PHP 文件中特定 .PHP 文件代码中的代码,它会返回相同的输出。通过使用 include() 或 require() 函数/语句,可以节省大量时间,并且还可以通过包含文件/内容来减少工作量。

只需将一些代码存储在特定的 .PHP 文件中,然后在需要时将其包含在任何其他 PHP 文件中,只需使用语法部分中提到的 include() 或 require () 语句即可。无需在需要时多次输入整个 PHP 代码,而是可以使用 include() 语句多次输入代码。例如,我们可以将 HEADER.PHP、MENU.PHP、FOOTER.PHP 文件包含在一个主 INDEX.PHP 文件中,以将这些 .PHP 文件中存在的代码包含在 INDEX.PHP 文件中。

实现 PHP Include 和 Require 的示例

以下是提到的示例:

示例#1

这是实现 PHP Include 语句的示例。首先创建html标签,然后在标题标签中输入一些字符串内容。当光标放置时,这将在浏览器选项卡上可见。然后是段落“

”标签用于输入一些内容,然后使用include语句在索引程序中导入HEADER1.PHP文件的内容。然后使用段落标签进行一些内容显示,然后再次使用include语句导入MENU1.PHP文件的内容。然后标题标签和段落标签与其中的一些内容一起使用。然后再次使用 PHP include 语句从 FOOTER1.PHP 文件导入内容。然后html标签就关闭了。包含该程序的语句将有助于获取其他 3 个 PHP 文件内容。

INDEX.PHP 文件的语法:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sake Pavan Kumar DOC - Implementing include() function </title>
</head>
<body>
<p>This content is from the Header1 PHP File :: </p>
<?php include "header1.php"; ?>
<p>This content is from the Menu1 PHP File :: </p>
<?php include "menu1.php"; ?>
<h1>Welcome to Our Blog/Website!</h1>
<p>Hi, This is Pavan Kumar Sake. I am a Content Writer, Blogger,
Engineer, Physics Nerd, Tech Enthusiast.
This content is from the original file only</p>
<p>This content is from the Footer1 PHP File :: </p>
<br/>
<?php include "footer1.php"; ?>
</body>
</html>

HEADER1.PHP 文件的语法:

<html>
<body>
<hr>
<?php
echo "Now you are in the Header.PHP files content!!"
?>
<hr>
</body>
</html>

MENU1.PHP 文件的语法:

<html>
<body>
<hr>
<?php
echo "Now you are in the Menu.PHP files content!!"
?>
<hr>
</body>
</html>

FOOTER1.PHP 文件的语法:

<html>
<body>
<hr>
<?php
echo "Now you are in the Footer.PHP files content!!"
?>
<hr>
</body>
</html>

输出:

PHP Include 和 Require 函数

示例#2

这是实现 PHP 编程语言 REQUIRE STATEMENT 的示例。这里我们不使用包含语句“include() 语句”,而是使用必需语句“require() 语句”。这里也和示例1一样,大部分内容是相同的,具有相同的HTML标签,并且一些字符串输出内容也相同,但区别仅在于REQUIRE STATEMENT OF PHP的用法。查看输出以便更好地理解。

INDEX.PHP 语法:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sake Pavan Kumar DOC - Implementing include() function </title>
</head>
<body>
<p><b>This is the PHP program which is implemented using the <u>REQUIRE STATEMENT</u></b></p>
<p>This content is from the Header1 PHP File :: </p>
<?php require "header1.php"; ?>
<p>This content is from the Menu1 PHP File :: </p>
<?php require "menu1.php"; ?>
<h1>Welcome to Pavan Kumar Sake Website!</h1>
<p>Hi, This is Pavan Kumar Sake. I am a Content Writer, Blogger,
Engineer, Physics Nerd, Tech Enthusiast.
This content is from the original file only</p>
<p>This content is from the Footer1 PHP File :: </p>
<br/>
<?php require "footer1.php"; ?>
</body>
</html>

HEADER1.PHP 语法:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require statement of PHP..";
echo "Now you are in the Header.PHP files content!!";
?>
<hr>
</body>
</html>

MENU1.PHP 的语法:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require statement of PHP..";
echo "Now you are in the Menu.PHP files content!!";
?>
<hr>
</body>
</html>

FOOTER1.PHP 的语法:

<html>
<body>
<hr>
<?php
echo "This is by implementing the require statement of PHP..";
echo "Now you are in the Footer.PHP files content!!";
?>
<hr>
</body>
</html>

输出:

PHP Include 和 Require 函数

PHP 的优点包括和要求

PHP的include和require语句在用来根据我们的需求开发静态和动态网页方面具有巨大的优势。这个 PHP 语句减少了工作量,并且在大多数情况下有助于节省大量时间。如果需要在特定文件或不同类型的 PHP 文件中输入某种类型的代码,则必须每次都键入,然后如果我们随时随地使用 include() 或 require() ,我们可以保存打字的时间也可以减少工作量。我们可以为每个 Web 开发部分使用单独的 PHP 文件。

结论

我希望您了解 PHP Include 和 Require 语句的定义及其语法和解释、如何在 PHP 中使用 Include 和 Require 以及各种示例、语句的优点等,以了解 PHP Include或者更好、更轻松地要求声明。

以上是PHP Include 和 Require 函数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP yield下一篇:PHP POST Method