首頁  >  文章  >  後端開發  >  PHP Include 與 Require 函數

PHP Include 與 Require 函數

WBOY
WBOY原創
2024-08-29 12:56:39175瀏覽

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 STATMENT 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 產量下一篇:PHP 產量