0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PHP】requireとrequire_once、includeとinclude_onceの違いまとめ

Posted at

require

ファイルを読み込む際に、ファイルが見つからないと、Fatal errorが発生し、スクリプトは実行停止する。
ファイルが必須な場合に使う。

require 'config.php';
echo "この行は表示される?"; 
//`config.php` がないと表示されない(Fatal errorで停止)

require_once

requireとほぼ同じだが、一度読み込まれたファイルを再度読み込まないようにできる。
クラス、関数の重複を防げる。

require_once 'config.php';
require_once 'config.php'; 
//2回目は無視される(エラーにならない)

include

ファイルを読み込む際に、ファイルが見つからないと、Warningを出すが、スクリプトは実行継続する。
ファイルが必須ではない場合に使う。

include 'config.php';
echo "この行は表示される!"; 
//`config.php` がなくてもこの行は実行される

include_once

includeとほぼ同じだが、一度読み込まれたファイルを再度読み込まないようにできる。
一度だけ必要なファイルを読み込む。

include_once 'config.php';
include_once 'config.php'; 
//2回目は無視される
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?