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?

More than 3 years have passed since last update.

php require とrequire_oneceの違い , includeとの違い, パスの指定方法についてなど

Last updated at Posted at 2021-02-24

includeとrequireの違いやパス指定のまとめ

違いについて

再読み込みを同じファイル内で行われた際の挙動が違う

参考:https://qiita.com/awesam86/items/3fa28e23c95ca74caddc

試してみた(index.phpでr.php, rone.phpを読み込み検証)

r.php

<?php 
$r = 'hoge'

rone.php

<?php 
$rone = 'hoge';

index.php

<?php 
require 'r.php';
require_once 'rone.php';

echo $r. "\n"; //hoge
echo $rone. "\n"; //hoge

$r = 'hoge_change'; 
$rone = 'hoge_change';

echo $r. "\n"; //hoge_change
echo $rone. "\n"; //hoge_change


require 'r.php'; 
require_once 'rone.php';

echo $r. "\n"; //hoge(requireではファイルの読み込みが再度行われるため値も$r = 'hoge'が再読み込みされている)
echo $rone. "\n"; //hoge_change(require_onceでは一度しか読み込まれないため表示がhoge_changeのまま)

includeとの違いは

エラー出た時requireは画面全体が消える。includeは警告の為消えずに他の画面が表示される。

参考:公式ドキュメント
include
スクリーンショット 2021-02-25 7.14.59.png

require
スクリーンショット 2021-02-25 7.14.51.png

パスの指定について

include時のパスの指定について
スクリーンショット 2021-02-25 7.14.25.png

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?