HOWTO · PHP

PHP 中的 require_once 與 include

本教程解釋並詳細說明如何以及何時在 PHP 檔案中使用 require、include、require_once 和 include_once。

本頁內容

對於開發人員,在開發週期中,我們在開發或生產目錄中分隔不同的 PHP 檔案,但我們可能需要另一個 PHP 檔案中的函式或類。在另一個檔案中重複相同的類或函式是違反直覺的,並且違反 DRY 方法。

因此,PHP 有四個關鍵字來新增或訪問另一個 PHP 檔案的內容,include、require、include_once 和 require_once。在本文中,我們將考慮所有關鍵字,但我們將更多地關注 require_once 和 include。

何時在 PHP 中使用 include 與 require 關鍵字

關鍵字 include 和 require 將所有內容、文字或程式碼或標記從指定的 PHP 檔案複製到包含語句的目標 PHP 檔案中。然而,include 關鍵字在處理失敗時的行為與 require 不同。

  • require 產生 fatal error (E_COMPILE ERROR) 並停止指令碼。
  • include 只產生一個 warning (E_WARNING),即使下一部分需要你包含的 PHP 檔案中的程式碼,指令碼也會繼續。

如果沒有發生錯誤,則兩個關鍵字的方式相同。

讓我們嘗試新增一個簡單的 PHP 檔案(variables.php),其中包含幾個變數到我們當前的 PHP 檔案中。

variables.php 檔案:

<?php

$team = 'Golden State Warriors';
$fav_player = 'Steph Curry';

?>

現在,讓我們使用 include 關鍵字來新增 variables.php 的內容:

<?php

echo "My favourite player is $fav_player and he plays for $team";

include 'variables.php';

echo "My favourite player is $fav_player and he plays for $team";

?>

上述程式碼片段的輸出:

My favourite player is and he plays for

My favourite player is Steph Curry and he plays for Golden State Warriors

如你所見,第一行沒有輸出變數的內容,因為我們沒有包含 variables.php 檔案,但是一旦我們新增它,它就有了變數的內容。

現在,讓我們使用 require 關鍵字:

<?php

echo "My favourite player is $fav_player and he plays for $team";

include 'variables.php';

echo "My favourite player is $fav_player and he plays for $team";

?>

輸出:

My favourite player is and he plays for

My favourite player is Steph Curry and he plays for Golden State Warriors

產生了相同的輸出。現在,讓我們在程式碼中拼錯 variables.php 檔案,看看效果。

對於 include 關鍵字:

My favourite player is and he plays for

Warning: include(variable.php): failed to open stream: No such file or directory in /home/runner/Jinku/index.php on line 5

Warning: include(): Failed opening 'variable.php' for inclusion (include_path='.:/nix/store/bq7pj5lz7rq92p3d3qyy25lpzic9phy5-php-7.4.21/lib/php') in /home/runner/Jinku/index.php on line 5

My favourite player is and he plays for

對於 require 關鍵字:

My favourite player is and he plays for

Warning: require(variable.php): failed to open stream: No such file or directory in /home/runner/Jinku/index.php on line 5

Fatal error: require(): Failed opening required 'variable.php' (include_path='.:/nix/store/bq7pj5lz7rq92p3d3qyy25lpzic9phy5-php-7.4.21/lib/php') in /home/runner/Jinku/index.php on line 5

你現在可以看到兩者之間的區別。使用 include 關鍵字,即使包含的檔案不存在並且只給出警告,程式碼也會繼續執行。

但是,使用 require 關鍵字時,程式碼會產生致命錯誤並在此時完全停止。

何時在 PHP 中使用 require_once 與 include 關鍵字

有適當的區域可以使用 include 或 require。建議一直使用 require;但是,如果你的應用程式可以在沒有我們打算新增到當前 PHP 檔案的 PHP 檔案中的內容的情況下執行,則可能不會出現問題。

為了提供更多上下文來理解 include、require 和 require_once 關鍵字,讓我們嘗試一個示例。對於這個例子,我們將建立一個 functions.php 檔案,該檔案將被包含在內並需要展示差異。

functions.php 檔案:

<?php

function add_sums($a, $b) {
  return $a + $b;
}

?>

讓我們使用 include:

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <?php
    include 'functions.php';

        echo "The program is starting\n";
    $sum = add_sums(2,3);

    echo "The program is done\n";
    echo $sum;
    ?>
  </body>
</html>

程式碼片段的輸出:

The program is done 5

讓我們使用 require:

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <?php
    require 'functions.php';

        echo "The program is starting\n";
    $sum = add_sums(2,3);

    echo "The program is done\n";
    echo $sum;
    ?>
  </body>
</html>

程式碼片段的輸出:

The program is done 5

但是,如果 functions.php 檔案不可用,而我們使用 include,則輸出如下:

Warning: include(functions.php): failed to open stream: No such file or directory in /home/runner/Jinku/index.php on line 7

Warning: include(): Failed opening 'functions.php' for inclusion (include_path='.:/nix/store/bq7pj5lz7rq92p3d3qyy25lpzic9phy5-php-7.4.21/lib/php') in /home/runner/Jinku/index.php on line 7

The program is starting

Fatal error: Uncaught Error: Call to undefined function add_sums() in /home/runner/Jinku/index.php:9 Stack trace: #0 {main} thrown in /home/runner/Jinku/index.php on line 9

儘管 functions.php 不存在,但程式碼仍在執行,直到由於呼叫未定義的函式而發生致命錯誤,這不是在 PHP 應用程式中工作的有效方法。

但是,當我們使用 require 時,錯誤如下所示:

Warning: require(functions.php): failed to open stream: No such file or directory in /home/runner/Jinku/index.php on line 7

Fatal error: require(): Failed opening required 'functions.php' (include_path='.:/nix/store/bq7pj5lz7rq92p3d3qyy25lpzic9phy5-php-7.4.21/lib/php') in /home/runner/Jinku/index.php on line 7

因為我們使用了 require 關鍵字,我們可以防止由於呼叫未定義函式而導致的致命錯誤。對於相當複雜的 PHP 應用程式,require 將更適合能夠快速捕獲錯誤並防止不必要的後果。

現在,讓我們更進一步。讓我們使用 require_once 關鍵字:

<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <?php
        // Previous code START
    require_once 'functions.php';

        echo "The program is starting\n";
    $sum = add_sums(2,3);

    echo "The program is done\n";
    echo $sum;
        // Previous code ENDS

        // Additional code STARTS

    echo "Another program begins";
    require 'functions.php';
    $sum = add_sums(2,3);
    echo $sum;
    echo "Another program ends";
    ?>
  </body>
</html>

上述程式碼片段的輸出會產生一個致命錯誤:

The program is starting
The program is done
5
Another program begins

Fatal error: Cannot redeclare add_sums() (previously declared in /home/runner/Jinku/functions.php:3) in /home/runner/Jinku/functions.php on line 3

require_once 關鍵字將檢查檔案是否已被包含,如果是,則不再包含或要求它。因此,因為我們之前使用了 require_once 關鍵字,它可以防止要求甚至包含 functions.php 中的程式碼。

你可能希望這樣做以防止重新宣告函式呼叫或變數值分配,這對你的程式碼來說可能代價高昂。

在示例中,如果我們使用 require 兩次,它將再次呼叫該函式併產生與之前相同的效果,結果為 5。

組合來理解。

  • require 和 require_once 將引發致命錯誤。
  • include 和 include_once 將引發致命錯誤。
  • require_once 和 require_once 可以正常工作,但所有內容將被召回和重新宣告。
  • include_once 和 include_once 可以正常工作,但所有內容將被召回和重新宣告。