PHP require 和 include 的差別

寫給自己看。

簡述

在網路上爬了一些文章發現內容都是錯的,先釐清幾個錯誤觀念

  1. require 不能用迴圈
1
2
3
4
5
6
7
8
$i = 1;
while ($i <= 3) {
require("./a_$i.php");
$i++;
}
echo '第一支檔案' . $a_1 . '<br>';
echo '第二支檔案' . $a_2 . '<br>';
echo '第三支檔案' . $a_3 . '<br>';

其實是 OK 的,我實測的結果如下:

1
2
3
第一支檔案a_1
第二支檔案a_2
第三支檔案a_3
  1. require 不能用 if else 做流程判斷
1
2
3
4
5
6
7
8
$is_true = TRUE;
if ($is_true) {
require("./a_1.php");
echo $a_1;
} else {
require("./a_2.php");
echo $a_2;
}

這一樣也可以,實測結果:

1
a_1

真正的差別

真正的差別在於,如果去引入不存在的檔案include 會噴 Warning,require 會噴 Fatal error。而 Waning 跟 Fatal error 的差別是會不會繼續往下執行程式碼。前者會,後者不會,就這麼簡單。

  1. require 不存在的檔案
1
2
require('./a_12345.php'); // 不存在的檔案
echo 'test';

輸出結果:

1
2
3
Warning: require(./a_12345.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\peanu\real-blog\main.php on line 4

Fatal error: Uncaught Error: Failed opening required './a_12345.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\peanu\real-blog\main.php:4 Stack trace: #0 {main} thrown in C:\xampp\htdocs\peanu\real-blog\main.php on line 4
  1. include 不存在的檔案
1
2
include('./a_12345.php'); // 不存在的檔案
echo 'test';

輸出結果:

1
2
3
4
Warning: include(./a_12345.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\peanu\real-blog\main.php on line 4

Warning: include(): Failed opening './a_12345.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\peanu\real-blog\main.php on line 4
test // <--- 有往下執行

眼尖的話會發現兩個都是先噴 Warning,接著才決定要噴 Warning 還是 Fatal error。

關於這部分節錄一下文件的解釋:

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn’t found in the include_path, include will finally check in the calling script’s own directory and the current working directory before failing. The include construct will emit an E_WARNING if it cannot find a file; this is different behavior from require, which will emit an E_ERROR.

Note that both include and require raise additional E_WARNINGs, if the file cannot be accessed, before raising the final E_WARNING or E_ERROR, respectively.

意思應該是說一開始會從我們給的路徑裡去找檔案,如果找不到的話 PHP 會從 include_path 給的值去找,如果還是找不到,會到目前執行腳本的資料夾再找一次,如果最後最後都還是找不到,include 就噴 Warning,require 就噴 Fatal Error。

所以上面的錯誤訊息才會有這一段:

1
Failed opening './a_12345.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\peanu\real-blog\main.php on line 4

另外就是不管最後是 Warning 還是 Fatal Error 在那之前都會先噴一個 Warning,所以才會看到兩個都有 Warning。

總結來說,如果程式一定要依賴 require 進來的東西來執行,最好是用 require,這樣讀檔失敗的時候就會直接停止,但如果是可有可無的話就能用 include,來確保程式不會跑到一半就被中斷。

有沒有 once 的差別

就是會不會重複引入而已,例如說:

  1. 沒有 once
1
2
3
4
5
6
7
8
9
10
11
12
// a_1.php
<?php
$a_1 = 'a_1';
echo $a_1 . '<br>';
?>

// main.php
<?php
include('./a_1.php');
include('./a_1.php');
include('./a_1.php');
?>

輸出結果:

1
2
3
a_1
a_1
a_1
  1. 有 once
1
2
3
4
5
6
7
8
9
10
11
12
// a_1.php
<?php
$a_1 = 'a_1';
echo $a_1 . '<br>';
?>

// main.php
<?php
include_once('./a_1.php');
include_once('./a_1.php');
include_once('./a_1.php');
?>

輸出結果:

1
a_1
認識 CSRF mentor-program-day70
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×